MySQL获得具有特定ID的最新结果?

时间:2013-03-18 08:07:51

标签: mysql

我关注了mySQL表

id   userid        document            created_date
___________________________________________________________
1  |     39 | 3985_doc_1363249277 | 2013-03-15 10:03:14
2  |     39 | 3985_doc_1363319837 | 2013-03-06 11:57:17
3  |     39 | 3985_doc_1363321078 | 2013-02-05 12:17:58
4  |     24 | 1524_doc_1363328910 | 2013-03-15 14:28:30
5  |     24 | 1524_doc_1363586975 | 2013-03-18 14:09:35

我想选择userid为'39'的最新文档。 具有created_date'2013-03-15 10:03:14'的文档将成为结果。

如何查询数据库以实现此结果?

7 个答案:

答案 0 :(得分:3)

SELECT *
FROM yourTableName
WHERE userid = 39
ORDER BY created_day DESC
LIMIT 1

答案 1 :(得分:2)

从table_name中选择*,其中userid = 39 order by created_date desc limit 0,1

答案 2 :(得分:1)

SELECT *
FROM table
WHERE userid = 39
ORDER BY created_day  DESC
LIMIT 1

此处LIMIT 1指定记录数

答案 3 :(得分:1)

SELECT  *                     // <<== select only the columns you want
FROM    tableName
WHERE   userid = 39
ORDER   BY created_date DESC
LIMIT   1

答案 4 :(得分:0)

试试这个,

SELECT document FROM TABLE WHERE userid = 39 ORDER BY created_date DESC LIMIT 0, 1;

答案 5 :(得分:0)

SELECT UserID, Max(Created_Date) from table Where UserID = 39 group by UserID

答案 6 :(得分:0)

MySQL,使用LIMIT:

SELECT * FROM table 
WHERE userid=3985 
ORDER BY created_date desc 
LIMIT 0,1