SQL连接同一表中的多个列


133

我有2个子查询,但是我无法将同一表中的列连接在一起。我试过了:

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;

如果我简单地以查询结束查询ON a.userid = b.sourceid就可以了,但是如何将这些表也连接到另一列上呢ON a.listid = b.destinationid

任何帮助表示赞赏。


2
感谢您的回答。.argh只是没有“;” 在查询结束时更早
user1899415

Answers:



65

您想加入条件1 AND条件2,因此只需使用AND关键字,如下所示

ON a.userid = b.sourceid AND a.listid = b.destinationid;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.