数据无法以适当的方式获得我想要的PostgreSQL多个内部加入

发布于 2025-01-21 15:38:35 字数 1015 浏览 3 评论 0原文

我试图在两个桌子上进行内部连接。 1。用户,2。骑行。在用户表中有两种类型的用户(用户为0,驱动程序为1)。在骑行桌上,我存储了来自用户表的User_id和suff_id。因此,现在问题是我无法同时获得两个用户的信息。

SELECT name,mobile, email, name as driverName
FROM users 
INNER JOIN rides  
ON users._id  = rides.user_id
INNER JOIN rides as ride 
ON users._id  = ride.provider_id;

这个返回我的桌子。

名称号码电子邮件驱动器

,当我只使用单个加入时:

SELECT name,mobile, email, name as driverName
FROM users 
INNER JOIN rides  
ON users._id  = rides.user_id```

 it return me name , mobile and email but not drivername.

| name | number | email |drivername |
|:---- |:------:| -----:|----------:|
| xyz  | 000000 | email |           |

i want ouput like this

| name | number | email |drivername |
|:---- |:------:| -----:|----------:|
| xyz  | 000000 | email |  abcsd    |

i.m trying to do a inner join on two table. 1. users , 2. rides. in user table there are two types of user(0 for user , 1 for driver). and in ride table i store user_id and provide_id which came from users table. so now problem is that i can't get information of both user simultaneously.

SELECT name,mobile, email, name as driverName
FROM users 
INNER JOIN rides  
ON users._id  = rides.user_id
INNER JOIN rides as ride 
ON users._id  = ride.provider_id;

this return me null table.

namenumberemaildrivername

and when i use only single join :

SELECT name,mobile, email, name as driverName
FROM users 
INNER JOIN rides  
ON users._id  = rides.user_id```

 it return me name , mobile and email but not drivername.

| name | number | email |drivername |
|:---- |:------:| -----:|----------:|
| xyz  | 000000 | email |           |

i want ouput like this

| name | number | email |drivername |
|:---- |:------:| -----:|----------:|
| xyz  | 000000 | email |  abcsd    |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2025-01-28 15:38:35

我可能倾向于使用存在的逻辑来表达您的查询:

SELECT u.name, u.mobile, u.email
FROM users u
WHERE EXISTS (SELECT 1 FROM rides r WHERE u._id = r.user_id) AND
      EXISTS (SELECT 1 FROM rides r WHERE u._id = r.provider_id);

这将返回每个用户和提供商乘车的用户。

I might be inclined to express your query using exists logic:

SELECT u.name, u.mobile, u.email
FROM users u
WHERE EXISTS (SELECT 1 FROM rides r WHERE u._id = r.user_id) AND
      EXISTS (SELECT 1 FROM rides r WHERE u._id = r.provider_id);

This will return every user for whom there is a both a user and provider ride.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文