我有一个消息栏,我想弄清楚我应该如何在“已发送”文件夹中显示消息。
我运行以下查询:
SELECT
`text`,
`created`
FROM
`messages`
WHERE
`status` = 1
ORDER BY `created` DESC
LIMIT 1
我想引入一个条件,以便只有当 status = 1
的最后一行也有 user_from = $user
时才返回结果(如果最后一行有 user_to = $user
,则不应返回任何内容)。
我的查询应该是什么?
最佳答案
您可以使用子查询
select `text`, `created` from T1
( SELECT
`text`,
`created`
FROM
`messages`
WHERE
`status` = 1
ORDER BY `created` DESC
LIMIT 1) T1
where `user_from` = $user and `user_to` != $user
关于mysql - 仅当满足 where 条件时才选择最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11988543/