简体   繁体   中英

Selecting two certain rows from a result table?

If my result rows are as follow:

ID    Name     Rate_Type
1     xxxx         9
2     zzzz         22
3     cccc         12
4     eeee         17
5     uuuu         90

Now how can I select the row with the rate type = 9 and any other row. I want my query to tell me if there is a row with Rate_type 9 and to give me also one row with ID <> 9

I want to have this result (only two row result):

ID    Name     Rate_Type
1     xxxx         9
!     !!!!         !--> This should be one additional row with Rate_Type <>9  

You can do this with the union of two queries. One that gets your rate_type = 9 , and one that gets your rate_type <> 9

(select * from t where rate_type = 9 order by rand() limit 1) 
union all
(select * from t where rate_type <> 9 order by rand() limit 1);

demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM