gpt4 book ai didi

MySQL连接两张表过滤数据

转载 作者:可可西里 更新时间:2023-11-01 08:39:46 25 4
gpt4 key购买 nike

我有两个表

第一个表:tblUser

userID  name    phone           email
1 A 1234567890 a@gmail.com
2 B 1234578569 b@gmail.com
3 C 1234567891 c@gmail.com
4 D 5456987452 d@gmail.com
5 E 1456987452 e@gmail.com

第二个表:tblShareLocation

userID  shareUserid shareLocation
1 2 0
2 3 1
1 3 1

我必须检查有多少用户已注册(为此,我将手机号码的检查列表放入 tblUser 表中,如果号码存在,这意味着该用户已注册)并检查有多少注册用户与 userID= 共享了他们的位置1

例如,如果我输入 userID=1 和手机号码 ('1234578569','1234567891','5456987452','1234567856')

Then the output are below
shareUserid name phone email shareLocation
2 B 1234578569 b@gmail.com 0
3 C 1234567891 c@gmail.com 1
4 D 5456987452 d@gmail.com 0

我想要上面的输出因为

  1. 对于输入,我给出了 4 个不同的数字,在 4 个数字中注册了三个,因此这三个数字及其详细信息显示在输出中
  2. userID=1 将他们的位置分享给 userID=3 这就是 shareLocation=1
  3. 的原因

我正在使用 MySQL。

最佳答案

您似乎需要一个简单的 LEFT JOIN 操作:

SELECT u.userID AS shareUserID, u.name, u.phone, u.email, 
COALESCE(sl.shareLocation, 0) AS shareLocation
FROM tblUser AS u
LEFT JOIN tblShareLocation AS sl ON u.userID = sl.shareUserID AND sl.userID = 1
WHERE phone IN ('1234578569','1234567891','5456987452','1234567856')

Demo here

关于MySQL连接两张表过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37874528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com