| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 10 | 1 | completed |2013-10-02|
| 3 | 3 | 11 | 6 | completed |2013-10-03|
表(用户)
Users_Id | Banned | Role |
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | driver|
如果用户角色需要是客户端并且这些客户端应该被取消禁止,我如何加入这两个表(旅程,用户)。 我正在尝试使用子查询,但我得到了#34;单行子查询返回多行"错误并且不确定出了什么问题。
SELECT * FROM Trips
INNER JOIN users
ON client_id = (SELECT users_id from users
where
role = 'Client' and banned = 'No');
答案 0 :(得分:2)
不需要子选择,只需将这些条件放入连接条件即可。
SELECT *
FROM Trips
JOIN users ON trips.client_id = users.users_id
and users.role = 'Client'
and users.banned = 'No';
由于这是一个内连接,这相当于:
SELECT *
FROM Trips
JOIN users ON trips.client_id = users.users_id
WHERE users.role = 'Client'
and users.banned = 'No';
如果您使用了外部联接
,那么不会会有所不同答案 1 :(得分:0)
你不需要子查询
联接应该在Trips和用户之间,例如:
SELECT *
FROM Trips
INNER JOIN users ON Trips.client_id = usres.users_id
and users.role = 'Client'
and users.banned = 'No';
答案 2 :(得分:0)
SELECT *
FROM Trips
INNER JOIN Users ON [Trips].Id = [Users].User_Id AND [Users].Role = 'client' AND [Users].Banned = 'No'
答案 3 :(得分:0)
尝试
NOT EXISTS