简体   繁体   English

MySQL if语句选择多行

[英]Mysql if statement to select multiple rows

I need to retrieve a set of data if a condition is true, and a set of other ata if the condition is false. 如果条件为true,则需要检索一组数据,如果条件为false,则需要检索其他一组ata。

I searched in mysql manual and I tested the select case when statement, but as my subqueries return multiple rows, I'm not able to use it. 我在mysql手册中进行了搜索,并测试了when语句时的select情况,但是由于我的子查询返回了多行,因此无法使用它。

Is it a simple way to write a query with if statement? 这是使用if语句编写查询的简单方法吗? (without using stored procedure) (不使用存储过程)

Here is my condition: 这是我的情况:

select case when ( right(from_unixtime(300 * floor(unix_timestamp(now())/300)) - INTERVAL 10 MINUTE, 8) = '23:50:00' ) then ( select SERVER_KEY from OVERSERVER ) else ( select SERVER_KEY from SERVER  ) end;

Thanks 谢谢

try something like this: 尝试这样的事情:

select SERVER_KEY from OVERSERVER
where right(from_unixtime(300 * floor(unix_timestamp(now())/300)) - INTERVAL 10 MINUTE, 8) = '23:50:00'
union all
select SERVER_KEY from SERVER
where right(from_unixtime(300 * floor(unix_timestamp(now())/300)) - INTERVAL 10 MINUTE, 8) != '23:50:00'

I am not sure but just give a try this- 我不确定,但是请尝试一下-

select SERVER_KEY from (case when ( right(from_unixtime(300 * floor(unix_timestamp(now())/300)) - INTERVAL 10 MINUTE, 8) = '23:50:00' ) then "OVERSERVER" else "SERVER"

OR 要么

select SERVER_KEY from (case when ( right(from_unixtime(300 * floor(unix_timestamp(now())/300)) - INTERVAL 10 MINUTE, 8) = '23:50:00' ) then OVERSERVER else SERVER

Note: above query is not tested. 注意:以上查询未经测试。

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

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