MySQL .. 如果 COUNT 返回大于 0 的任何值,则返回“1”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6234561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:07:45  来源:igfitidea点击:

MySQL.. Return '1' if a COUNT returns anything greater than 0

mysqlsql

提问by kimion09

I'm trying to create MySQL query that essentially returns true or false. I'd like to run

我正在尝试创建基本上返回 true 或 false 的 MySQL 查询。我想跑

SELECT COUNT(id) 
  FROM comments  
 WHERE comment_date >= 1306904400 
   AND user_id = 1 

So if the user posted in the forum 10 times this month, I'd like it to return just 1, otherwise I'd like it to return 0 to indicate they haven't.

因此,如果用户本月在论坛上发帖 10 次,我希望它只返回 1,否则我希望它返回 0 以表明他们没有。

Is that possible efficiently within SQL?

这在 SQL 中是否可能有效?

回答by mu is too short

If you don't mind MySQL-specific things then you could use IF:

如果您不介意特定于 MySQL 的事情,那么您可以使用IF

select if(count(id) >= 10, 1, 0)
from comments
where comment_date >= 130690440
  and user_id = 1

Or MySQL booleans(which are 1 for true and 0 for false):

MySQL 布尔值(1 表示真,0 表示假):

select count(id) >= 10
from comments
where comment_date >= 130690440
  and user_id = 1

If you want to stick to standard SQL, then CASEis your friend:

如果您想坚持使用标准 SQL,那么CASE您的朋友是:

select case when count(id) >= 10 then 1 else 0 end
from comments
where comment_date >= 130690440
  and user_id = 1

回答by Xint0

Use CASE:

使用CASE

SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS `flag`
FROM `comments`
WHERE `comment_date` >= 1306904400 AND `user_id` = 1

回答by Michael Dean

You don't need to count all the matching records, just find if one exists. Unfortunately, MySQL doesn't appear to support IF EXISTSas Microsoft SQL Server does. However, you can use a subselect with LIMITto fake it:

您不需要计算所有匹配的记录,只需查找是否存在。不幸的是,MySQL 似乎不像IF EXISTSMicrosoft SQL Server 那样支持。但是,您可以使用子选择 withLIMIT来伪造它:

SELECT COUNT(1) AS has_comments FROM (SELECT id FROM comments WHERE comment_date >= 1306904400 AND user_id = 1 LIMIT 0, 1) t;

回答by Hyman

I know this is an old thread, but here's another way to accomplish this:

我知道这是一个旧线程,但这是实现此目的的另一种方法:

SELECT COUNT(id), 1 % (1 + count(id)) as greaterThanZero FROM comments
WHERE comment_date >= 1306904400 AND user_id = 1

SELECT COUNT(id), 1 % (1 + count(id)) as greaterThanZero FROM comments
WHERE comment_date >= 1306904400 AND user_id = 1

回答by Rucent88

I use a LIMIT to do something similar, and just evaluate it in my code. The SQL will stop after finding 10 matches.

我使用 LIMIT 来做类似的事情,并在我的代码中对其进行评估。找到 10 个匹配项后,SQL 将停止。

SELECT id FROM {mytable}
WHERE comment_date >= 1306904400
AND user_id = 1
LIMIT 10

PHP requesting a minimum of 10 posts.

PHP 要求至少 10 个帖子。

if(my_db_query() != 10)
    not_enough_posts();

回答by jcomeau_ictx

perhaps:

也许:

SELECT (SELECT COUNT(id) FROM comments  
WHERE comment_date >= 1306904400 AND user_id = 1) > 10