Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL join on multiple columns in same tables

Tags:

sql

join

People also ask

How do I join two different columns in the same table in SQL?

The following example shows how to concatenate three different columns: (SELECT id, email1 AS email FROM customer) UNION (SELECT id, email2 AS email FROM customer) UNION (SELECT id, email3 AS email FROM customer) ORDER BY id, email; As you can see, it's important that all the queries return the same columns.

Can I inner join on multiple columns?

Yes: You can use Inner Join to join on multiple columns.

Can we apply join on same table?

The self-join is a special kind of joins that allow you to join a table to itself using either LEFT JOIN or INNER JOIN clause. You use self-join to create a result set that joins the rows with the other rows within the same table.


You need to replace the second ON with AND, like this:

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

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

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