赞
踩
- SELECT o.product_id,name,quantity,o.unit_price
- from order_items o -- 别名 o
- inner join products p -- inner可省略
- on o.product_id=p.product_id -- 条件
- SELECT o.product_id,name,quantity,o.unit_price
- from order_items o -- 别名 o
- inner join products p -- inner可省略
- on o.product_id=p.product_id -- 条件
- use sql_hr;
- select e.employee_id,e.first_name,e.last_name,
- m.employee_id as manager_id
- from employees e -- 使用别名后,再引用表名只能使用别名
- join employees m on e.reports_to=m.employee_id
- select c.client_id,invoice_id,date,amount,p.payment_method
- from payments p
- join payment_methods pm on p.payment_id=pm.payment_method_id
- join clients c on p.client_id=c.client_id;
- select *
- from order_items oi
- join order_items_notes oin
- on oi.order_id=oin.order_Id and oi.product_id=oin.product_id
- select o.customer_id,o.order_id,c.first_name,s.name as shipper
- from orders o
- join customers c
- -- on o.customer_id=c.customer_id 代替
- using (customer_id)
- left join shippers s
- -- on o.shipper_id=s.shipper_id 代替
- using (shipper_id)
- -- 显式
- select *
- from orders o
- join customers c on o.customer_id=c.customer_id;
- -- 隐式
- select *
- from orders o,customers c
- where o.customer_id=c.customer_id;
- select p.product_id,name,quantity
- from products p
- -- left join order_items oi on p.product_id=oi.product_id;
- right join order_items oi on p.product_id=oi.product_id;
- select order_date,order_id,first_name,s.shipper_id as shipper,os.name as status
- from orders o
- left join customers c on c.customer_id=o.customer_id
- left join shippers s on o.shipper_id=s.shipper_id
- left join order_statuses os on os.order_status_id=o.status
- select
- s.employee_id,
- s.first_name,
- m.employee_id as manager
- from employees s
- left join employees m on s.reports_to=m.employee_id
- select *
- from orders o
- natural join customers c
- -- 合并同表的几段查询,根据分数不同,划分金、银、铜
- select customer_id,
- first_name,
- points,
- 'Gold' as type
- from customers c
- where points>=3000
- union
- select customer_id,
- first_name,
- points,
- 'Silver' as type
- from customers c
- where points >=2000 and points <3000
- union
- select customer_id,
- first_name,
- points,
- 'Bronze' as type
- from customers c
- where points<2000
- order by points desc
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。