将两个mysql查询与两个不同的order by语句组合在一起

时间:2015-06-01 05:54:45

标签: mysql

我想结合这两个查询。

   SELECT * FROM table1 WHERE status='pending' and adr='' order by id desc limit 0,1;

   SELECT * FROM table1 where status='pending' and adr='new' ORDER BY RAND() LIMIT 1

1 个答案:

答案 0 :(得分:0)

您可以使用UNION ALL集合运算符来连接两个查询的结果

( SELECT * FROM table1 WHERE status='pending' AND adr='' ORDER BY id DESC LIMIT 1 )
UNION ALL
( SELECT * FROM table1 WHERE status='pending' AND adr='new' ORDER BY RAND() LIMIT 1 )

参考:UNION ALL https://dev.mysql.com/doc/refman/5.5/en/union.html