编写一个返回与特定用户相关的最新行的查询

时间:2016-08-03 08:57:57

标签: php mysql sql

尝试创建一个获取与用户相关的最新记录的查询。这是我的尝试

"SELECT * 
 FROM (
    SELECT *
    FROM mytable
    ORDER BY id
    DESC LIMIT 1)
 WHERE userid = $userID";

4 个答案:

答案 0 :(得分:2)

你也可以:

Select * from table 
Where id =
   (Select Max(Id) from table
    where userid = $userID)

答案 1 :(得分:1)

只需跳过子查询:

SELECT * 
FROM  mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1

答案 2 :(得分:0)

使用热门(1)

Select TOP(1) * FROM table  ORDER BY id DESC

答案 3 :(得分:0)

尝试:

SELECT *
    FROM mytable
    WHERE userid = $userID
    ORDER BY id DESC
    LIMIT 1