我有一个UNION SQL查询,可以生成无序的行集。结果与之相同
+----+----------------------+-------+ | id | name | value | +----+----------------------+-------+ | 3 | John | 21 | +----+----------------------+-------+ | 2 | Jim | 5 | +----+----------------------+-------+ | 4 | Adrian | 25 | +----+----------------------+-------+ | 1 | Sam | 22 | +----+----------------------+-------+
答案 0 :(得分:1)
MySql中缺少rownum
函数的常用技巧:
SELECT
@rownum:=@rownum+1 AS rownum,
id, name, value
FROM
(
(SELECT id, name, value FROM buyers ORDER BY id)
UNION
(SELECT id, name, value FROM seller ORDER BY id)
) subq,
(SELECT @rownum:=0) r;
然后使用rownum
字段在行之间导航。