同一表中多列的 SQL 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16597660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL join on multiple columns in same tables
提问by user1899415
I have 2 subqueries, but I'm having trouble joining columns together from the same tables. I tried:
我有 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;
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
??
如果我只是简单地用ON a.userid = b.sourceid
它结束查询,但是我怎样才能在另一列上加入这些表呢ON a.listid = b.destinationid
??
Any help appreciated.
任何帮助表示赞赏。
回答by Zoran Horvat
Join like this:
像这样加入:
ON a.userid = b.sourceid AND a.listid = b.destinationid;
回答by Paul McLoughlin
You want to join on condition 1 AND condition 2, so simply use the AND keyword as below
你想加入条件 1 AND 条件 2,所以只需使用 AND 关键字如下
ON a.userid = b.sourceid AND a.listid = b.destinationid;