桌子
id string
------------
1 aaa
2 bbb
3 ccc
4 ddd
查询
(SELECT string FROM table WHERE id > 1 ORDER BY id ASC LIMIT 1) /* num_row = 1 */
UNION
(SELECT string FROM table WHERE id < 1 ORDER BY id DESC LIMIT 1) /* null */
UNION
(SELECT string FROM table WHERE id > 4 ORDER BY id ASC LIMIT 1) /* null */
UNION
(SELECT string FROM table WHERE id < 4 ORDER BY id DESC LIMIT 1) /* num_row = 2 */
上面的查询将返回
2 rows
,因为没有id = 5和id = 0。我如何知道从这两行中取哪些查询?
即
num_row = 1
中的1st SELECT
和num_row = 2
中的4th SELECT
最佳答案
使用第二列指示数据来自何处
(SELECT string, '1st query' as from_where FROM table WHERE ...)
UNION
(SELECT string, '2nd query' as from_whereFROM table WHERE ...)