码迷,mamicode.com
首页 > 数据库 > 详细

mysql: join

时间:2018-06-27 23:54:28      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:select   HERE   sele   app   from   exist   javascrip   out   class   

/***************
 * join tables *
 ***************/
 
-- list all sales for FLEX and BLAZE;
select * from product;
select * from sales;
-- what happens without a.product_id=b.product_id;
-- use table alias;

 
 
-- practice: list all transactions with Amazon;
select s.*,c.*
from sales s, client c
where s.client_id = c.client_id and c.name = ‘Amazon‘;


-- list all sales with msrp > 100 and quantity >= 10, or quality >= 100; ignore non-existing product_id in PRODUCT;
-- ignore non-existing product_id in PRODUCT;
select s.*,p.*
from sales s, product p
where s.product_id = p.product_id and ((p.msrp > 100 and s.quantity >= 10) or (s.quantity >= 100));


/***************
*     Join     * 
***************/

-- show all transactions with shipping status;
/* inner join */
select s.*,sh.*
from sales s
inner join shipping sh
where s.tran_id = sh.tran_id;

/* left join */
select s.*,sh.*
from sales s
left join shipping sh
on s.tran_id = sh.tran_id;

-- list all shipments with shipping status; show client_id and product_id if exists;
/* right join */
select s.*,sh.*
from sales s
right join shipping sh
on s.tran_id = sh.tran_id;

-- list all shipping status with client_id and product_id;
/* right join */

-- list all sales and shipping status in one table;


-- list all transactions and shipping status in one table;
/* full join */
-- select s.tran_id as trn_id, s.client_id, s.product_id, sh.status
-- from shipping sh
-- full join sales s
-- on s.tran_id = sh.tran_id
-- ;
-- MySQL doesn‘t support full join!


/*********************
 *    set operators  *
 *********************/
-- union;
select * from sales;
select * from sales where tran_id > 1
union
select * from sales where tran_id > 2;

-- union all
select * from sales;
select * from sales where tran_id > 1
union all
select * from sales where tran_id > 2;

  

mysql: join

标签:select   HERE   sele   app   from   exist   javascrip   out   class   

原文地址:https://www.cnblogs.com/tabCtrlShift/p/9236444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!