mysql必知必会
首先有个表
顾客表:产品表:
订单元素表:产品记录表:
订单表:
卖家表:
第十六章 创建高级联结
select concat(rtrim(vend_name),'(',rtrim(vend_country),')') as vend_title from vendors order by vend_name;
使用别名来进行,有两种好处:
1. 缩短sql语句
2. 允许在单条select语句中多次使用相同的表
使用不同类型的联结
1. 自联结
自己和自己进行表的联结
select p1.prod_id,p1.prod_name from products as p1,products as p2 where p1.vend_id = p2.vend_id and p2.prod_id = 'DTNTR'
从上面的代码可以看出来:使用表的别名进行相互之间的表的关联
2. 自然联结
根据各个表之间的字段进行表之间的联结
select c.*,o.order_num,o.order_date,oi.prod_id,oi.quantity,oi.item_price from customers as c, orders as o,orderitems as oi where c.cust_id = o.cust_id and oi.order_num = o.order_num and prod_id = 'FB'
3.外部联结