如何为每个 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/