从表中获取最后200条记录

时间:2015-05-30 10:50:02

标签: java php mysql sql

我在数据库中有1000多条记录。我需要一个查询来只选择最后200条记录。

SELECT 
      c.member_id, c.message_id, fm.firstname AS fname, up.gender,  
      TIMESTAMPDIFF( YEAR, up.dob, NOW( ) ) AS age, c.upload_img, c.image, 
      c.message_id AS msg_id, c.message AS msg, c.posted_timestamp AS time, 
      c.topic_id AS topic_id, u.croppedpicture_filename,u.picture_filename FROM 
      conversation_messages_tbl c, user_profileinformation_tbl up, user_tbl u, family_member_tbl fm 
WHERE c.member_id = fm.family_member_id AND
      up.user_id = fm.user_id AND 
      u.emailid = fm.emailid AND 
      c.topic_id = '1' 
      GROUP BY c.message_id desc LIMIT 200 sub ORDER BY c.message_id ASC

此查询无法执行。

1 个答案:

答案 0 :(得分:2)

包含order by子句以获取最后200条记录。

SELECT c.member_id,c.message_id, fm.firstname AS fname, up.gender, 
TIMESTAMPDIFF( YEAR, up.dob, NOW( ) ) AS age, c.upload_img, c.image, 
c.message_id AS msg_id, c.message AS msg, c.posted_timestamp AS time, 
c.topic_id AS topic_id, u.croppedpicture_filename,u.picture_filename 
FROM conversation_messages_tbl c, user_profileinformation_tbl up, 
user_tbl u, family_member_tbl fm WHERE c.member_id = 
fm.family_member_id AND up.user_id = fm.user_id AND u.emailid = 
fm.emailid AND c.topic_id = '$topicId' GROUP BY c.message_id desc 
ORDER BY c.member_id DESC LIMIT 200