mysql - 选择每个 id 的 in() 返回相等的行数

标签 mysql sql where-clause

如何为每个 user_id 选择行等于为每个 user_id 选择计数?

我的示例表:

mp3_id  |  user_id
--------------------
120     |  840
123     |  840
126     |  840
128     |  455
130     |  840
131     |  840
132     |  840
135     |  840
144     |  840
158     |  840
159     |  455
161     |  455
169     |  455
180     |  840
181     |  455
184     |  455
186     |  455
189     |  455

我的简单查询:

select mp3_id where user_id IN (840,455) limit 8

返回:

mp3_id  |  user_id
--------------------
120     |  840
123     |  840
126     |  840
128     |  455
130     |  840
131     |  840
132     |  840
135     |  840

但我想要这个选择:

mp3_id  |  user_id
--------------------
120     |  840
123     |  840
126     |  840
130     |  840
128     |  455
159     |  455
161     |  455
169     |  455

我希望每个 user_id 返回相等的行数。怎么办?

最佳答案

你可以用 UNION 来做到这一点:

select mp3_id where user_id = 840 limit 4
union all
select mp3_id where user_id = 455 limit 4

关于mysql - 选择每个 id 的 in() 返回相等的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44584703/

相关文章:

c# - 构建动态 where 子句,Linq To Sql

Mysql 查询 - 显示产品的第二个分类法阻止了我

java - WSO2-wso2am-2.1.0 API 管理器!!!无法启动 wso2 API 管理器

mysql - 使用MySQL将两个日期时间的差异插入到另一个表中

sql - case when in where 子句 Postgresql

c# - 如何使用 "Group by"LINQ?

php - 无法使用 phpseclib 在 MySQL 中运行 SQL 查询

MySQL左连接优化

MySQL 触发新旧查询

java - 在 Java 中执行 SQL 时处理所有异常