mysql 取一行_mysql – 查询只从具有相同值的多行中获取一行

要获取MySQL中的最新行,您需要使用连接或相关子查询:

SELECT id, user_receiver, user_sender, post_id, action, date, is_read

FROM notification n

WHERE user_receiver=$ses_user and

date = (select max(date)

from notification n2

where n2.user_sender = n.user_sender and

n2.action = n.action and

n2.post_id = n.post_id and

n2.is_read = n.is_read

)

order by date desc;

在其他数据库中,您只需使用row_number()函数(或Postgres中的distinct).

编辑:

对于最大的id:

SELECT id, user_receiver, user_sender, post_id, action, date, is_read

FROM notification n

WHERE user_receiver=$ses_user and

id = (select max(id)

from notification n2

where n2.user_sender = n.user_sender and

n2.action = n.action and

n2.post_id = n.post_id

)

order by date desc;

如果你想要isread = 1的行数,那么你可以这样做:

SELECT sum(is_read = 1)

FROM notification n

WHERE user_receiver=$ses_user and

id = (select max(id)

from notification n2

where n2.user_sender = n.user_sender and

n2.action = n.action and

n2.post_id = n.post_id

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值