在同一表中的多个列上的SQL连接

时间:2020-12-06 06:05:59

I have 2 subqueries, but I'm having trouble joining columns together from the same tables. I tried:

我有两个子查询,但是我无法将来自相同表的列连接在一起。我试着:

SELECT * FROM

(SELECT userid, listid 
FROM user_views_table
WHERE date='2013-05-15' AND view_type='lists') a

JOIN

(SELECT sourceid, destinationid
FROM actions_table
WHERE date='2013-05-15' AND payloadtype='lists_user' AND actiontype='delete') b

ON a.userid = b.sourceid
ON a.listid = b.destinationid;

If I simply end the query with ON a.userid = b.sourceid it works, but how can I also join these tables on another column also ON a.listid = b.destinationid ??

如果我用a结束查询。用户id = b。sourceid它是可以工作的,但是我如何才能在a的另一列中加入这些表呢?listid = b.destinationid ? ?

Any help appreciated.

任何帮助表示赞赏。

2 个解决方案

#1


116  

Join like this:

加入这样的:

ON a.userid = b.sourceid AND a.listid = b.destinationid;

#2


39  

You want to join on condition 1 AND condition 2, so simply use the AND keyword as below

您希望在条件1和条件2上加入,因此只需如下所示使用AND关键字

ON a.userid = b.sourceid AND a.listid = b.destinationid;

#1


116  

Join like this:

加入这样的:

ON a.userid = b.sourceid AND a.listid = b.destinationid;

#2


39  

You want to join on condition 1 AND condition 2, so simply use the AND keyword as below

您希望在条件1和条件2上加入,因此只需如下所示使用AND关键字

ON a.userid = b.sourceid AND a.listid = b.destinationid;