当前位置:   article > 正文

表连接的几种方式

表连接

 自连接:

能够在单独一条SELECT语句内多次引用同一个表。

  1. select prod_id,prod_name
  2. from products
  3. where vend_id = ( select vend_id
  4. from products
  5. where prod_id='DTNTR')
  1. select prod_id,prod_name
  2. from products p1,products p2
  3. where p1.vend_id = p2.vend_id
  4. and p2.prod_id='DTNTR'

自然连接:

  1. select c.*,o.order_num,o.order_date
  2. from customer c,orders o
  3. where c.cust_id=o.cust_id
  4. and prod_id='FB'
  1. select c.*,o.order_num,o.order_date
  2. from customer c,orders o
  3. on c.cust_id=o.cust_id
  4. where
  5. prod_id='FB'

左外连接:

  1. select c.*,o.order_num,o.order_date
  2. from customer
  3. LEFT OUTER JOIN orders
  4. on customer.cust_id=orders.cust_id
  5. where
  6. prod_id='FB'

右外连接:

  1. select c.*,o.order_num,o.order_date
  2. from customer
  3. RIGHT OUTER JOIN orders
  4. on customer.cust_id=orders.cust_id
  5. where
  6. prod_id='FB'

可以把左外连接转换成右外连接,只需要在FROM或where子句中点到表的顺序即可。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号