MySQL LEFT JOIN and RIGHT JOIN

Published: 16 November 2022
on channel: spa it
56
5

DROP DATABASE base2;
CREATE DATABASE base2;
USE base2;
CREATE TABLE Customers
(
Id INT PRIMARY KEY AUTO_INCREMENT,
Age INT,
FirstName VARCHAR(20) NOT NULL,
LastName VARCHAR(20) NOT NULL,
Phone VARCHAR(20) NOT NULL UNIQUE
);


INSERT Customers(Age, FirstName, LastName, Phone)
VALUES (18,'Ganna','Velickiv','0688889977');
INSERT Customers(Age, FirstName, LastName, Phone)
VALUES (19,'Ury','Medvejuy','0688889988');
INSERT Customers(Age, FirstName, LastName, Phone)
VALUES (39,'Anton','Chernov','0687889988');
INSERT Customers(Age, FirstName, LastName, Phone)
VALUES (22,'Anna','Ignatss','0687885988');
INSERT Customers(Age, FirstName, LastName, Phone)
VALUES (32,'Olga','Hiv','0681885988');

********************************************************
Створюємо та заповнюємо Orders

CREATE TABLE Orders
(
Id INT PRIMARY KEY AUTO_INCREMENT,
CustomerId INT,
CreatedAt Date,
Details VARCHAR(20)
);



INSERT Orders(CustomerId, CreatedAt, Details)
VALUES (2, Now(), 'The Thrill is Gone');
INSERT Orders(CustomerId, CreatedAt, Details)
VALUES (1, Now(), 'Savoy Brown Blues');
INSERT Orders(CustomerId, CreatedAt, Details)
VALUES (5, Now(), 'BILLIE JEAN');
*********************Orders2**********************

Створюємо та заповнюємо Orders2


CREATE TABLE Orders2
(
Id INT PRIMARY KEY AUTO_INCREMENT,
CustomerId INT,
CreatedAt Date,
Details VARCHAR(20)
);



INSERT Orders2(CustomerId, CreatedAt, Details)
VALUES (2, Now(), 'Close to You');
INSERT Orders2(CustomerId, CreatedAt, Details)
VALUES (1, Now(), 'Savoy Brown Blues');
INSERT Orders2(CustomerId, CreatedAt, Details)
VALUES (3, Now(), 'If You Leave Me Now');


Так виглядає Customers
mysql -e 'select * from base2.Customers'

Так виглядає Orders
mysql -e 'select * from base2.Orders'

Так виглядає Orders2

mysql -e 'select * from base2.Orders2'

SELECT *
FROM Orders
RIGHT JOIN Orders2 ON Orders.CustomerId=Orders2.CustomerId;


SELECT *
FROM Orders2
RIGHT JOIN Orders ON Orders.CustomerId=Orders2.CustomerId;


SELECT *
FROM Orders
LEFT JOIN Orders2 ON Orders.CustomerId=Orders2.CustomerId;

SELECT *
FROM Orders2
LEFT JOIN Orders ON Orders.CustomerId=Orders2.CustomerId;


Watch video MySQL LEFT JOIN and RIGHT JOIN online without registration, duration 05 minute 12 second in high hd quality. This video was added by user spa it 16 November 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 5 once and liked it people.