Friday 28 September 2018

mtsql - Inner join & Outer join

こんにちは!今日はInner Join とOuter Joind です。

- Inner Join

select vend_name, prod_name, prod_price
from venders inner join products on venders.vend.id = products.vend_id;

- Outer join


1. Left join

   select customers.cust_id, orders.order_num
    from customers left join orders
    on customer.cust_id = orders.cust_id;
  全てのcustomersのレコードが対象となる(左のテーブル)。customersのレコード全てが出力され、customer.cust_id = orders.cust_idに引っかからないorders.order_num(右側のレコード)はNullが表示される。

2.  Right join

    select customers.cust_id, orders.order_num
    from customers right join orders
    on customer.cust_id = orders.cust_id;
1.の逆。ordersのレコードが全て表示される(右のテーブル)(orders.order_num)。
customer.cust_id = orders.cust_idでないcustomerのレコードはNullと表示される。

- Full Outer Join

  select customers.cust_id, orders.order_num
  from orders full outer join customers
  on orders.cust_id = customers.cust_id;
2つのテーブルの全てのレコードが表示される。orders.cust_id = customers.cust_idに当てはまらない場合は、そのレコードにはNullと表示される。

お知らせ:もし、誤りがあれば、コメントしていただくとありがたいです。
↓来て頂きありがとうございます。クリックしていただくと、励みになります。↓
にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

No comments:

Post a Comment