Inner Join Example 3 Tables Syntax MySQL



Inner Join Example 3 Tables Syntax MySQL

--inner join example 3 tables...

select customer.cust_code, name, town, orders.order_no, order_date
from customer
inner join orders
   on customer.cust_code = orders.cust_code
inner join order_detail
   on orders.order_no = order_detail.order_no;


select c.cust_code, name, town, o.order_no, order_date
from customer c
inner join orders o
   on c.cust_code = o.cust_code
inner join order_detail d
   on o.order_no = d.order_no;


select customer.cust_code, name, town, orders.order_no, order_date
from customer
join orders
   on customer.cust_code = orders.cust_code
join order_detail
   on orders.order_no = order_detail.order_no;

Inner Join Example 2 Tables Syntax MySQL

--inner join example 2 tables...

select customer.cust_code, name, order_no
from customer
inner join orders
on customer.cust_code = orders.cust_code;

Test tables

create table customer (
cust_code varchar(20),
name varchar(20),
town varchar(20),
address_line_1 varchar(20)
);

insert customer ( cust_code, name, town, address_line_1) values 
( "B023", "Boots",  "Nottingham", "23 Moor Street");
insert customer ( cust_code, name, town, address_line_1) 
values ( "K045", "Kelloggs",  "Birmingham", "412 Broad Street");

drop table orders;
create table orders (
order_no int,
cust_code varchar(20),
order_date varchar(20)
);

insert orders ( order_no, cust_code, order_date) 
values ( 1002, "B023", "4 Nov 2016");
insert orders ( order_no, cust_code, order_date) 
values ( 1003, "K045", "5 Nov 2016");

create table order_detail (
order_no int,
product_code varchar(20),
qty int
);

insert order_detail ( order_no, product_code, qty )
values ( 1002, "PH091", 78 );
insert order_detail ( order_no, product_code, qty )
values ( 1003, "PH095", 1245 );
insert order_detail ( order_no, product_code, qty )
values ( 1004, "PH045", 276 );

Page created on 3rd Nov 2016
Push Symbol. Push Symbol. Door logos