mysql 同一字段满足多个条件

前言

一对多,以多为查询条件

正文

举例:
两张表,用户表,物品表。一个用户可以有多个物品,根据拥有的物品去查询用户。

t_user表
user_id user_name
1 小王
2 小刘
t_goods表
g_id g_name
1 手机
2 电脑
3 书包
4
5 电视
6 游戏机
t_user_goods
user_id g_id
1 1
1 2
1 4
1 6
2 2
2 5

要根据选择的物品,来筛选出拥有这些物品的人。

-- 传入的参数分别是 物品id列表 和 物品id列表的数量
select * from t_user
where user_id in (
	select user_id from t_user_goods
	where g_id in (?)
	group by user_id having count(user_id) >= ?
)
原文地址:https://www.cnblogs.com/wmg92/p/13637586.html