Can we use the join operation and make restrictions at the same time? I want tocreate a table on a Database (called DB1 in the example) based on two tables from Database another database (called DB2 in the example) where one of the columns is filled when there´s a specific entry in on other column (in the example a"4" in column "gender"). -> If yes, then how to do ?
Both databases are on the same server and DBMS is the same. ID1 and ID2 based on table1 in DB2; ID1 and Name based on table2 in DB2.
Here´s what I tried out already but I´m not sure if it works and/or its the best way Made from point of view that I´m using DB1:
INSERT INTO table3
(id1,
id2,
NAME)
SELECT t1.id1,
t1.id2,
t2.NAME
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID1=t2.ID1
WHERE gender = 4;
Or is it correct (better) with AND?:
INSERT INTO table3
(id1,
id2,
NAME)
SELECT t1.id1,
t1.id2,
t2.NAME
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID1=t2.ID1
AND gender = 4;