当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - How to select data from table with different conditions in the same query

问题描述:

I have a table, I want selcect data from this table with condition type =1 , type =2, type =3 and each type limit by 5.

If I used the following command, 3 table join to each other ); so I have 5x5x5 = 125 record!

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

)

How can I select this data with just 1 command so I have table with 15 records?

网友答案:
(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)
网友答案:

You should use union statement

  (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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: