如何在同一查询中从具有不同条件的表中选择数据

时间:2014-10-30 09:36:43

标签: mysql

我有一张桌子,我希望此表中的selcect数据条件为 type =1 , type =2, type =3 ,每种类型的限制为5。

如果我使用以下命令,则3个表互相连接);所以我有5x5x5 = 125记录!

SELECT * 
FROM 
( 
  ( select * from `question` where type = 1 limit 5) as t1 , 
  (select * from `question` where type = 2 limit 5) as t2, 
  (select * from `question` where type = 3 limit 5) as t3
)

如何只用1个命令选择这个数据,所以我有15个记录的表?

2 个答案:

答案 0 :(得分:3)

(SELECT * FROM question WHERE type = 1 LIMIT 5)
UNION
(SELECT * FROM question WHERE type = 2 LIMIT 5)
UNION
(SELECT * FROM question WHERE type = 3 LIMIT 5)

答案 1 :(得分:0)

您应该使用union语句

  (select * from question where type = 1 limit 5)
  UNION
  (select * from question where type = 2 limit 5)
  UNION
  (select * from question where type = 3 limit 5)
相关问题