SQL COUNT() WHERE 多个值

发布于 2024-11-28 22:13:57 字数 622 浏览 2 评论 0原文

我将简短地写这篇文章:我想计算我的行出现在表中的次数,其中该行是特定值,而另一行可能是 4 个不同的值。我将发布我的“猜测”,但它不起作用:

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


有关表的信息

表名称
英雄

行数
Hero_type

在何处选择“hero_type”
值为“agi”

仅在“hero_selected”所在的位置选择
ytybymyf

如果可能的话,我也希望包含 yb 的完整代码>echo php 中的计数金额。提前非常感谢! :)

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-12-05 22:13:58
SELECT COUNT(*) 
FROM heroes 
WHERE hero_type = 'agi' AND hero_selected IN ('yt', 'yb', 'ym', 'yf')
SELECT COUNT(*) 
FROM heroes 
WHERE hero_type = 'agi' AND hero_selected IN ('yt', 'yb', 'ym', 'yf')
轮廓§ 2024-12-05 22:13:58

从我从你的问题中收集到的信息,你想要这个查询

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

PHP 代码可能看起来像这样,

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

我在这里假设你只想要一个计数,而不是像其他答案那样每个不同的 hero_selected 值一个计数表明

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文