深蓝色的风 2015-07-11 11:13 采纳率: 0%
浏览 2965

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语句怎么写?

  • 写回答

5条回答 默认 最新

  • 霓为衣兮风为裳 2015-07-11 11: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

    评论
  • garegradf 2015-07-11 12: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 2015-07-11 13: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 2015-07-11 13: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)

    评论
  • 丵鹰 2015-07-17 14: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)

    评论

报告相同问题?

悬赏问题

  • ¥30 计算机网络子网划分路由模拟操作
  • ¥15 MATLAB的画图问题
  • ¥15 c语言用fopen_s成功打开文件之后闪退
  • ¥20 用C++完成,并且运用数组
  • ¥30 求解电力系统潮流计算结果不收敛问题
  • ¥15 某易易盾点选data解析逆向
  • ¥15 系统崩溃,关于订单的处理
  • ¥15 datax-web连接hive为数据源时发生报错,如何解决?
  • ¥15 plink在进行gwas分析时总读取不到表型
  • ¥20 数据结构与c语言的实践内容