国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL left join 左連接詳解介紹

2024-07-24 12:38:45
字體:
來源:轉載
供稿:網友

LEFT JOIN 關鍵字會從左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中沒有匹配的行,left join 左連接——以左邊的數據表為基準,若右邊的數據表沒有對應的數據則顯示空白.

  1. 表a 
  2. 1 aid adate   
  3. 2 1 a1   
  4. 3 2 a2   
  5. 4 3 a3  
  6. 表b 
  7. 1 bid bdate   
  8. 2 1 b1   
  9. 3 2 b2   
  10. 4 4 b4 

代碼如下:select * from a left join b on a.aid = b.bid

首先取出a表中所有數據,然后再加上與a、b匹配的的數據,此時的取出的是:

  1. 1 1 a1 b1   
  2. 2 2 a2 b2   
  3. 3 3 a3 空字符  

同樣的也有right join,指的是首先取出b表中所有數據,然后再加上與a、b匹配的的數據,此時的取出的是:

  1. 1 1 a1 b1   
  2. 2 2 a2 b2 

left join性能

1例子,代碼如下:

  1. select  distinct count('goods_id'as num , g.goods_id,g.goods_name from ms_goods as g left  join ms_order_goods as og on g.goods_id = og.goods_id group by goods_id 

顯示行 0 - 29 (1,475 總計,查詢花費 0.0167 秒)

2例子,代碼如下:

  1. select  distinct count('goods_id'as num , g.goods_id,g.goods_name from ms_goods as g , ms_order_goods as og where g.goods_id = og.goods_id group by goods_id 

顯示行 0 - 29 (257 總計,查詢花費 0.0088 秒).

實例,表結構如下:

  1. MDate      StoreCode  GoodsCode  ToStoreCode  GoodsAmount  GoodsFlag 
  2. 2005-12-12   001             101                  888                         2            3 
  3. 2005-12-13   001             101                     3                          1 
  4. 2005-12-15   001             101                     1                          2 

三條記錄的意思分別為:

2005-12-12從001移到888  2件        

2005-12-13從001銷售     3件

2005-12-15倉店001進貨   2件

出報表

  1. StoreCode  GoodsCode  StockAmount   InAmount     OutAmount    SellAmount 
  2. 001              101                       3                  0                      2             1  

注:StockAmount進貨數量   InAmount移入數量     OutAmount移出數量    SellAmount銷售數量,代碼如下:

  1. select  
  2.     StoreCode, 
  3.     GoodsCode, 
  4.     StockAmount = sum(case GoodsFlag when 2 then GoodsAmount else 0 end), 
  5.     InAmount    = sum(case GoodsFlag when 4 then GoodsAmount else 0 end), 
  6.     OutAmount   = sum(case GoodsFlag when 3 then GoodsAmount else 0 end), 
  7.     SellAmount  = sum(case GoodsFlag when 1 then GoodsAmount else 0 end)  --Vevb.com 
  8. from 
  9.     表 
  10. group by 
  11.     StoreCode,GoodsCode 

例子3:

  1. --po_order_det 表 
  2. ID   MA_ID     QTY 
  3. 01   #21鋼     30   
  4. 02   #22鋼     40 
  5. 03   #23鋁     30 
  6. --ST_CONVER 表 
  7. ID  QTY  
  8. 01  20 
  9. 02  10 

要求返回集 QTY = po_order_det.QTY - ST_CONVER.QTY AND po_order_det.ID=ST_CONVER.ID

  1. ID     MA_ID   QTY 
  2. 01     #21鋼   10 
  3. 02     #22鋼   30 
  4. 03     #23鋁    30 

代碼如下:

  1. select a.order_id,a.id,a.ma_id,a.qty,isnull(b.qty,0) qtyy, isnull(a.qty  - b.qty ,0) qtyx 
  2. from po_order_det a left join st_conver b  
  3. on a.id=b.id and a.filid=b.filid  
  4. and a.id=b.id and a.order_id=b.order_id  
  5. Where  a.filid='S' 

總結,SQL 中只出現一個左連接,代碼如下:

  1.  select a.*, b.*, c.* from b, a 
  2. left join c on a.id = c.id 
  3. where b.id = a.iid 

注意 表a 要和最近的一個關聯的leftjoin挨著.

SQL中出現了2個或者多個左連接,代碼如下:

  1. select a.*, b.* c.*, d.*, e.* 
  2. from (( 
  3.    c, b, a 
  4.    left join d on d.id = a.id 
  5. )  left join e  on e.id = b.id 
  6. --Vevb.com 
  7. where c.id = b.id 
  8. and b.id = a.id 

leftjoin總和最近的一個表挨著,同時需要用借助于括號.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丽水市| 淮滨县| 武清区| 锡林浩特市| 自治县| 沂源县| 尤溪县| 清河县| 富源县| 淮南市| 塔城市| 皋兰县| 剑阁县| 东城区| 依兰县| 曲阜市| 辉南县| 嘉定区| 大邑县| 大港区| 工布江达县| 盱眙县| 阿图什市| 泰州市| 繁昌县| 陆丰市| 留坝县| 莲花县| 涞水县| 绥中县| 洛隆县| 浮山县| 肥西县| 景谷| 连州市| 榕江县| 高州市| 白沙| 五指山市| 额尔古纳市| 榆社县|