How to join two tables by multiple columns in SQL?
Yes: You can use Inner Join
to join on multiple columns.
SELECT E.CaseNum, E.FileNum, E.ActivityNum, E.Grade, V.Score from Evaluation E
INNER JOIN Value V
ON E.CaseNum = V.CaseNum AND
E.FileNum = V.FileNum AND
E.ActivityNum = V.ActivityNum
Create table
CREATE TABLE MyNewTab(CaseNum int, FileNum int,
ActivityNum int, Grade int, Score varchar(100))
Insert values
INSERT INTO MyNewTab Values(CaseNum, FileNum, ActivityNum, Grade, Score)
SELECT E.CaseNum, E.FileNum, E.ActivityNum, E.Grade, V.Score from Evaluation E
INNER JOIN Value V
ON E.CaseNum = V.CaseNum AND
E.FileNum = V.FileNum AND
E.ActivityNum = V.ActivityNum
pandas: merge (join) two data frames on multiple columns
Try this
new_df = pd.merge(A_df, B_df, how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2'])
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html
left_on : label or list, or array-like Field names to join on in left
DataFrame. Can be a vector or list of vectors of the length of the
DataFrame to use a particular vector as the join key instead of
columnsright_on : label or list, or array-like Field names to join on
in right DataFrame or vector/list of vectors per left_on docs
join on multiple columns
Agree no matches in your example.
If you mean both columns on either then need a query like this or need to re-examine the data design.
Select TableA.Col1, TableA.Col2, TableB.Val
FROM TableA
INNER JOIN TableB
ON TableA.Col1 = TableB.Col1 OR TableA.Col2 = TableB.Col2
OR TableA.Col2 = TableB.Col1 OR TableA.Col1 = TableB.Col2
Efficient way to join multiple columns to the same column? - SQL
You might find it easier to use a correlated subquery for each username, especially where you have to implement many columns as it's easier to cut n paste!
Something like:
select
(select Username from Mapping m where m.UserId = t.UserId1) Username1,
(select Username from Mapping m where m.UserId = t.UserId2) Username2,
(select Username from Mapping m where m.UserId = t.UserId3) Username3 etc
from InputTable t
How to do an INNER JOIN on multiple columns
You can JOIN with the same table more than once by giving the joined tables an alias, as in the following example:
SELECT
airline, flt_no, fairport, tairport, depart, arrive, fare
FROM
flights
INNER JOIN
airports from_port ON (from_port.code = flights.fairport)
INNER JOIN
airports to_port ON (to_port.code = flights.tairport)
WHERE
from_port.code = '?' OR to_port.code = '?' OR airports.city='?'
Note that the to_port
and from_port
are aliases for the first and second copies of the airports
table.
SQL left outer join on multiple columns
That depends on whether the columns are nullable, but assuming they are not, checking any of them will do:
SELECT *
FROM a
LEFT JOIN b
ON a.foo = b.foo
AND a.bar = b.bar
AND a.ter = b.ter
WHERE b.foo IS NULL -- this could also be bar or ter
This is because after a successful join, all three columns will have a non-null value.
If some of these columns were nullable and you'd like to check if any one of them had a value after the join, then your first (OR
) approach would be OK.
SQL join on multiple columns in same tables?
You can implement it as a JOIN
with two independent JOIN
s to CODE_TABLE
, one for each value in DATA_TABLE
:
SELECT A.COLUMN1,
B1.CODE_NAME AS COLUMN2,
B2.CODE_NAME AS COLUMN3
FROM DATA_TABLE A
JOIN CODE_TABLE B1 ON B1.CODE = A.COLUMN2
JOIN CODE_TABLE B2 ON B2.CODE = A.COLUMN3
If it's possible that a value from COLUMN2
or COLUMN3
in DATA_TABLE
might not exist in CODE_TABLE
, you should use LEFT JOIN
instead, and COALESCE
on the output so as not to return a NULL
value:
SELECT A.COLUMN1,
COALESCE(B1.CODE_NAME, '') AS COLUMN2,
COALESCE(B2.CODE_NAME, '') AS COLUMN3
FROM DATA_TABLE A
LEFT JOIN CODE_TABLE B1 ON B1.CODE = A.COLUMN2
LEFT JOIN CODE_TABLE B2 ON B2.CODE = A.COLUMN3
Output (for your sample data)
COLUMN1 COLUMN2 COLUMN3
test APPLE LG
test1 SAMSUNG HWAWEI
test2 OPPO GOOGLE
Demo on dbfiddle
Related Topics
Select Multiple Rows with the Same Value(S)
Table in Excel from SQL Server Stored Procedure with Parameter Field in Workbook
Boolean 'Not' in T-SQL Not Working on 'Bit' Datatype
Update Existing Database Values from Spreadsheet
Convert 24 Hour Time to 12 Hour Plus Am/Pm Indication Oracle SQL
Ora-01843 Not a Valid Month- Comparing Dates
Parse JSON into Oracle Table Using Pl/Sql
Performance Difference: Condition Placed at Inner Join VS Where Clause
How to Set Server Output on in Datagrip
SQL Code to Insert Multiple Rows in Ms-Access Table
Most Executed Stored Procedure
Make All Store Images the Base, Small and Thumbnail Images in Magento
What's the Escape Sequence for Hyphen (-) in Postgresql
How to Convert a Bunch of Boolean Columns to a Single Bitmap in Postgresql
Iterate Through Rows in SQL Server 2008
Postgresql: Create Table If Not Exists As
Why Is Parameterized SQL Generated by Nhibernate Just as Fast as a Stored Procedure
How to Give a Unique Constraint to a Combination of Columns in Oracle