简体   繁体   English

从结果表中选择两个特定的行?

[英]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. 现在,如何选择费率类型= 9的行以及其他任何行。 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 我想让我的查询告诉我是否存在Rate_type 9的行,并给我一行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. 您可以通过两个查询的union来实现。 One that gets your rate_type = 9 , and one that gets your rate_type <> 9 一个让您的rate_type = 9 ,另一个让您的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 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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