Shanshan Pythoner Love CPP

SQL JOIN

2016-12-05
SQL

-- INNER JOIN to get the full info as long as one is in one of the two tables
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

-- LEFT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

-- RIGHT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

-- FULL JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

-- UNION
-- UNION SELECT RESULTS
-- UNION ALL ALLOW THE DUPLICATE

select distinct a.company_id from (select distinct company_id from ebd_stgcustoms_com_e) a
inner join (select distinct company_id from ebd_stgcustoms_com_i) b
on a.company_id = b.company_id limit 1000

-- Three table joins
select b.id, c.province, a.amount from table1 b join table2 c on b.id = c.id join table3 a on a.province = c.province

Comments

Content