[英]How to use OR in MySQL query and ignore second condition if first is true?
I am using a query like this: 我正在使用这样的查询:
SELECT * FROM table1 WHERE countries LIKE '%US%' OR countries LIKE '%ALL%';
This returns all the entries that have "US" or "ALL" in countries column, but I want it to return ONLY those rows that contains "US", and if non is found then use the next condition and return all that contains "ALL". 这将返回在国家列中具有“ US”或“ ALL”的所有条目,但我希望它仅返回包含“ US”的那些行,如果未找到,则使用下一个条件并返回包含“ ALL”的所有条目”。
Is it possible to do this in only one query? 只能在一个查询中执行此操作吗?
By using a sub-query: 通过使用子查询:
SELECT *
FROM table1
WHERE countries LIKE (CASE WHEN (SELECT COUNT(*)
FROM table1
WHERE countries LIKE '%US%') = 0
THEN '%ALL%'
ELSE '%US%'
END)
我认为不可能仅使用一个SQL查询。
if you are using mysql, Though i have't try this, you can try this. 如果您使用的是mysql,虽然我还没有尝试过,但是您可以尝试一下。
SELECT IF(field1 is null, field2, field1) FROM table1;
here field1 is your "Countries like US" and field2 is "Countries like ALL" . 在这里,field1是您的“像美国一样的国家”,field2是“像所有国家一样的国家”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.