V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
shelken
V2EX  ›  问与答

一条 sql 问题,实在不知道如何解?

  •  
  •   shelken · 2022-11-21 09:34:09 +08:00 · 1132 次点击
    这是一个创建于 539 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用户

    {
    	id: 1,
    	name: a
    }
    

    其他平台用户

    {
    	id: 4,
    	userId: 1
    	userType: 个人
    }
    
    {
    	id: 5,
    	userId: 1
    	userType: 法人
    }
    

    用户表 1 对多 其他平台用户表 如何只查询了关联了个人的系统用户,以及只关联了法人的系统用户,以及同时关联个人和法人的系统用户?不使用 count 等函数方法,只用 where 条件或者 on 条件一条 sql 查询。

    是否有解?

    2 条回复    2022-11-21 10:57:01 +08:00
    InTen
        1
    InTen  
       2022-11-21 09:45:55 +08:00
    select u.id,u.name,u1.id as id1,u1.userType as userType1 ,u2.id as id2,u2.userType as userType2
    from user
    left join other_user u1 on user.id=u1.id and u1.userType='个人'
    left join other_user u2 on user.id=u2.id and u1.userType='法人'
    shelken
        2
    shelken  
    OP
       2022-11-21 10:57:01 +08:00
    @InTen 谢谢,大佬
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1443 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:18 · PVG 01:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.