简单 SQL 联合 2 查询相同 table

Simple SQL Union 2 queries on the same table

正在尝试获取 userid = 'me'

的所有行

然后 2 个最新行,其中 userid <> 'me'

项目:

Userid time
other2   11
other3   10
me       10
me        8
other1    8
other3    7
me        6

会return

Userid  time
me        10
me         8
me         6
other2    11
other3    10

结果不必按任何顺序排列

(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items  WHERE userid<>'me' 
ORDER BY time DESC  LIMIT 2)

这只输出 2 行

如上所示 sql 是正确的

(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items  WHERE userid<>'me' 
ORDER BY time DESC  LIMIT 2)

Union All 连接两个查询,括号允许不同的 WHERE/ORDER BY/LIMIT 子句。
此处演示: http://sqlfiddle.com/#!9/ca405/1/0