简体   繁体   English

SQL COUNT() WHERE 多个值

[英]SQL COUNT() WHERE multiple values

I'll make this post short: I want to count the number of times my row appears in my table where the row is a specific value And another row could be 4 different values.我会让这篇文章简短:我想计算我的行出现在我的表中的次数,其中该行是一个特定的值,而另一行可能是 4 个不同的值。 I'll post my "guess" which doesn't work:我将发布我的“猜测”,但它不起作用:

SELECT hero_selected, COUNT(hero_type) FROM heroes WHERE hero_type = 'agi' AND hero_selected = 'yt' OR hero_selected = 'yb' OR hero_selected = 'ym' OR hero_selected = 'yf


INFORMATION ABOUT THE TABLE关于表的信息

TABLE NAME表名
heroes英雄

ROW TO COUNT行数
hero_type hero_type

SELECT "hero_type" WHERE SELECT "hero_type" WHERE
Value is 'agi'价值是“敏捷”

ONLY SELECT IT WHERE "hero_selected" IS只有 SELECT 它在哪里“hero_selected”是
Either yt , yb , ym or yf ytybymyf

If possible I'd also appreciate the full code including an echo of the count amount in php.如果可能的话,我也会欣赏完整的代码,包括php中计数数量的回显。 Thanks A LOT in advance: :)提前非常感谢::)

SELECT COUNT(*) 
FROM heroes 
WHERE hero_type = 'agi' AND hero_selected IN ('yt', 'yb', 'ym', 'yf')

From what I can glean from your question, you want this query从我可以从你的问题中收集到的,你想要这个查询

SELECT COUNT(1) FROM heroes
WHERE hero_type = 'agi'
AND hero_selected IN ('yt', 'yb', 'ym', 'yf')

PHP code might look like this PHP 代码可能如下所示

$db = new PDO(...);
$stmt = $db->query($query);
$count = $stmt->fetchColumn();
echo $count;

I'm assuming here that you only want a single count, not one per different hero_selected value as the other answers are indicating我在这里假设您只想要一个计数,而不是每个不同的hero_selected值一个,因为其他答案表明

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

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