返回与mysql中没有重复的其他条件匹配的最新行和行

时间:2015-06-09 22:24:01

标签: mysql

我的数据看起来像这样

Table "People"

+----------+-------------+----------------------------------+
|id        |age          |name        |job        |gender   |
+-----------------------------------------------------------+
|1         |26           |joe         |Welder     |m        |
|2         |50           |Mary        |Juggler    |f        |
|3         |26           |Henry       |Dancer     |m        |
|4         |31           |Pat         |Trucker    |f        |
|5         |26           |Frank       |Designer   |m        |
+----------+-------------+------------+-----------+---------+

我想返回最后两个条目,所以我尝试SELECT * ORDER by id DESC LIMIT 2,但这也会返回年龄超过30岁的人,而不会返回重复的行。

我的预期结果将是Frank,Pat和Mary。

1 个答案:

答案 0 :(得分:0)

如果您仍需要答案:

(SELECT id, age, name
 FROM People 
 ORDER BY ID DESC
 LIMIT 2)
UNION
(SELECT id, age, name
 FROM People
 WHERE Age>30)