Home > Article > Backend Development > 求mysql一条语句,该如何处理
求mysql一条语句
按id降序查询几种情况的各一条记录,用一条语句能否实现?
------解决方案--------------------
union 可不可以的,没用过
------解决方案--------------------
对
(select * from a order by id desc limit 0,1) union all (select * from b order by id desc order by id desc limit 0,1)
------解决方案--------------------
可以啊,用UNION ALL
------解决方案--------------------
按情况分组 每组取一条记录
SELECT * FROM (SELECT * FROM `table` ORDER BY `condition` DESC) t GROUP BY `condition`;<div class="clear"> </div>