从第一顺序仅获取一条记录,从第二顺序仅获取一条记录

时间:2018-12-22 12:56:27

标签: mysql sql

我有一个查询

SELECT s.*
     , g.* 
  from tbl_section1 as s
     , tbl_game as g 
  LEFT 
  JOIN tbl_game_visit_count AS gvc 
    ON g.game_id = gvc.game_id 
 where s.category_id = g.game_id 
 ORDER 
    BY g.udate DESC
     , gvc.visit_count DESC

效果很好。

但是我想获取g.udate排序的第一个记录,然后获取gvc.visit_count排序的其余记录。

使用mysql查询可以吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

可以在两个带有UNION和{的查询之间使用UNION ALL不是ORDER BY,因为我们不想重复行)括号内的{1}}子句

LIMIT

P.S。由于您的原始查询,我为SELECT q.* FROM ( SELECT s.*, g.* FROM tbl_section1 as s INNER JOIN tbl_game as g ON s.category_id = g.game_id LEFT JOIN tbl_game_visit_count AS gvc ON g.game_id = gvc.game_id ORDER BY g.udate DESC LIMIT 1 ) q UNION SELECT s.*, g.* FROM tbl_section1 as s INNER JOIN tbl_game as g ON s.category_id = g.game_id LEFT JOIN tbl_game_visit_count AS gvc ON g.game_id = gvc.game_id ORDER BY gvc.visit_count DESC; 保留了DESC个选项,如果要定期升序排序,可以删除它们。