mysql - 请求Select时如何计算列表中的条目数?
问题描述
对于演示文稿中可能存在的错误,我深表歉意,我使用了翻译。假设有一个用户表,其中有一个 id 字段。并且有一个列表列出了 id 编号,其中一些是重复的。我的查询
select id, count(*)
from users
where id in (3, 10, 10, 10)
group by id;
返回以下 3 - 1、10 - 1。我想得到 3 - 1、10 - 3,依此类推。有没有可能以某种方式得到它?UPD。列表中的数据 (3, 10, 10, 10) 只是一个示例,确切的位数未知,因为它们是从另一个问题返回的。
解决方案
您将需要使用join
. 为此,您可以将值放在派生表中:
select id, count(*)
from users u join
(select 3 as id union all
select 10 as id union all
select 10 as id union all
select 10 as id union all
) i
using(id)
group by id;
推荐阅读
- list - Haskell中N个列表的所有元素组合
- reactjs - 如何键入作为 prop 传递给 React 组件的异步函数
- ms-access - MS Access 2013:多次加入表
- python - 无法用python替换字节中的十六进制
- c# - 如何替换 SQL 文件中的“CREATE VIEW”、“CREATE PROCEDURE”、“CREATE FUNCTION”、“CREATE TRIGGER”?
- angular - 错误:无法读取 null 的属性“classList”(角度)
- java - 使用 Jackson 删除具有特定键、值的嵌套 JSON 对象
- twitter-bootstrap - 两列不保持在行中(引导程序)
- python - 无法格式化字符串 - Python
- swift - Firestore 在尝试创建文档时崩溃