MYSQL IF SELECT COUNT()大于零select select * from table else返回任何未找到的内容

时间:2014-02-21 05:23:59

标签: mysql select if-statement count where

我正在寻找一种方法来查询:

MySQL的:

示例:IF SELECT COUNT(column_XX) WHERE column_XX = value_XX FROM table_XX大于零(0)然后SELECT所有内容或SELECT * FROM table_XX WHERE column_XX = value_XX返回所有数据......否则返回&#39;找不到任何内容&#39; 或只是ZERO(0)< / p>

简单来说,如果列中的所有值都大于零,我想从表列中选择所有数据,否则只选择值&gt; = 1并返回所有值。

单一查询中可以应用哪个魔术?...我真的非常感谢。

2 个答案:

答案 0 :(得分:3)

你可以尝试这样:

SELECT * FROM table_XX
WHERE column_XX = (
  SELECT column_XX FROM table_XX 
  WHERE column_XX = value_XX GROUP BY column_XX HAVING COUNT(column_XX) > 0
)

答案 1 :(得分:0)

这会有用吗?

SELECT * FROM test_enum;
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
|    1 | Abdul | Yes      |
+------+-------+----------+
2 rows in set (0.00 sec)

SELECT * FROM test_enum WHERE EXISTS(SELECT * FROM test_enum WHERE IsActive = 'No') AND IsActive = 'No';
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
+------+-------+----------+
1 row in set (0.00 sec)