简体   繁体   中英

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. 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

SELECT "hero_type" WHERE
Value is 'agi'

ONLY SELECT IT WHERE "hero_selected" IS
Either yt , yb , ym or yf

If possible I'd also appreciate the full code including an echo of the count amount in 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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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