2 panwei321 panwei321 于 2015.07.11 19:13 提问

mysql查询-怎么判断一个用户在多个表中是否有值

场景如下,
有一张用户表user,主键用户编号user_id ,类型int; 三张业务表 a_record,b_record,c_record,分别都含有外键user_id.

在方法中,别人传给我一串user_id ,字符串的,逗号分割的,如“1001,2001,2003,98”;
我需要去数据库中判断这些user_id串中的用户是否有三种业务中至少一条的记录,
(只要这个user_id字符串中的有一个人有其中之一业务记录,我就可以返回true,否则返回false )

这个sql语句怎么写?

7个回答

huxiaohuahua
huxiaohuahua   2015.07.11 21:50

如果user中 的user_id 是字符串的,如“1001,2001,2003,98”:
select count(*) from user u,a_record a,b_record b,c_record c
when a.user_id in (u.user_id) or a.user_id in (u.user_id) or a.user_id in (u.user_id)

huxiaohuahua
huxiaohuahua 回复深蓝色的风: 客气了
2 年多之前 回复
panwei321
panwei321 3Q!
2 年多之前 回复
tabe123
tabe123   Rxr 2015.07.11 19:46

select num(user,id) from user,a_record where user_id = a_record
union
select num(user,id) from user,a_record where user_id = b_record
union
select num(user,id) from user,a_record where user_id = c_record

u012896290
u012896290   2015.07.11 20:09

select count(1) from user u,a_record a,b_record b,c_record c where u.user_id=a.user_id and u.user_id=b.user_id and u.user_id=c.user_id

huxiaohuahua
huxiaohuahua   2015.07.11 21:32

select count(*) from user u,a_record a,b_record b,c_record c
when u.user_id=a.user_id or u.user_id=b.user_id or u.user_id=c.user_id

huxiaohuahua
huxiaohuahua   2015.07.11 21:32

select count(*) from user u,a_record a,b_record b,c_record c
when u.user_id=a.user_id or u.user_id=b.user_id or u.user_id=c.user_id

huxiaohuahua
huxiaohuahua   2015.07.11 21:32

select count(*) from user u,a_record a,b_record b,c_record c
when u.user_id=a.user_id or u.user_id=b.user_id or u.user_id=c.user_id

panwei321
panwei321 这个可以,不过数据量大了后性能下降的厉害!不过还是谢谢!
2 年多之前 回复
strutce
strutce   Ds   Rxr 2015.07.17 22:56

select user,id from user,a_record where user_id in(1000,1001,1002)
union
select user,id from user,a_record where user_id in (1000,1001,1002)
union
select user,id from user,a_record where user_id in (1000,1001,1002)

Csdn user default icon
上传中...
上传图片
插入图片