根據表字段值left join 不同的表

2021-08-14 02:54:18 字數 4589 閱讀 3524

首先說下思路吧,搗鼓了好半天,網上找到解決辦法,大概兩種方法:

1.在left join 的後面將兩張表union all連線為一張表,並加上「區分表關鍵字」,然後根據「區分表關鍵字」篩選我們需要的資料

2.使用兩個left join, 每個left join 跟上一張表,並加上「區分表關鍵字」,然後在 on 條件後使用「區分表關鍵字」

重點

下面是sql語句

第一種:乙個left join 將兩張表union all

select 

ord.*,

odtl_goods_id, odtl_goods_type, odtl_quantity,

con_company_name,

mer_company,

goods.goodsname, goods.goodspicture

from

`order`

as ord

left

join order_detail as odtl on odtl_order_id=ord.order_id

left

join consumer as con on con.con_id=ord.order_con_id

left

join merchant as mer on mer.mer_id=ord.order_mer_id

left

join (

select

1as goodstype, sh_id as goodsid, sh_picture as goodspicture, sh_name as goodsname from server_hosting

union

allselect

2as goodstype, sr_id as goodsid, sr_picture as goodspicture, sr_name as goodsname from server_rental

) as goods on goods.goodstype=odtl.odtl_goods_type and goods.goodsid=odtl_goods_id

sr.sr_id=odtl.odtl_goods_id)

where ord.order_id=1

第一種:使用兩個left join

select 

ord.*,

odtl_goods_id, odtl_goods_type, odtl_quantity,

con_company_name,

mer_company,

ifnull(goods1.goodsname,goods2.goodsname) as goodsname, ifnull(goods1.goodspicture,goods2.goodspicture) as goodspicture

from

`order`

as ord

left

join order_detail as odtl on odtl_order_id=ord.order_id

left

join consumer as con on con.con_id=ord.order_con_id

left

join merchant as mer on mer.mer_id=ord.order_mer_id

left

join (select

1as goodstype, sh_id as goodsid, sh_picture as goodspicture, sh_name as goodsname from server_hosting) as goods1 on goods1.goodstype=odtl.odtl_goods_type and goods1.goodsid=odtl_goods_id

left

join (select

2as goodstype, sr_id as goodsid, sr_picture as goodspicture, sr_name as goodsname from server_rental) as goods2 on goods2.goodstype=odtl.odtl_goods_type and goods2.goodsid=odtl_goods_id

where order_id=1

參考

首先說下思路吧,網上找了,大概兩種方法:

1.在left join 的後面將兩張表union all連線為一張表,並加上「區分表關鍵字」,然後根據「區分表關鍵字」篩選我們需要的資料

2.使用兩個left join, 每個left join 跟上一張表,並加上「區分表關鍵字」,然後在 on 條件後使用「區分表關鍵字」

重點

left join 後面只能跟一張表,不能使用if,when case 啥的

新增「區分表關鍵字」進行條件判斷

下面是sql語句

第一種:乙個left join 將兩張表union all

select 

ord.*,

odtl_goods_id, odtl_goods_type, odtl_quantity,

con_company_name,

mer_company,

goods.goodsname, goods.goodspicture

from

`order`

as ord

left

join order_detail as odtl on odtl_order_id=ord.order_id

left

join consumer as con on con.con_id=ord.order_con_id

left

join merchant as mer on mer.mer_id=ord.order_mer_id

left

join (

select

1as goodstype, sh_id as goodsid, sh_picture as goodspicture, sh_name as goodsname from server_hosting

union

allselect

2as goodstype, sr_id as goodsid, sr_picture as goodspicture, sr_name as goodsname from server_rental

) as goods on goods.goodstype=odtl.odtl_goods_type and goods.goodsid=odtl_goods_id

sr.sr_id=odtl.odtl_goods_id)

where ord.order_id=1

第一種:使用兩個left join

select 

ord.*,

odtl_goods_id, odtl_goods_type, odtl_quantity,

con_company_name,

mer_company,

ifnull(goods1.goodsname,goods2.goodsname) as goodsname, ifnull(goods1.goodspicture,goods2.goodspicture) as goodspicture

from

`order`

as ord

left

join order_detail as odtl on odtl_order_id=ord.order_id

left

join consumer as con on con.con_id=ord.order_con_id

left

join merchant as mer on mer.mer_id=ord.order_mer_id

left

join (select

1as goodstype, sh_id as goodsid, sh_picture as goodspicture, sh_name as goodsname from server_hosting) as goods1 on goods1.goodstype=odtl.odtl_goods_type and goods1.goodsid=odtl_goods_id

left

join (select

2as goodstype, sr_id as goodsid, sr_picture as goodspicture, sr_name as goodsname from server_rental) as goods2 on goods2.goodstype=odtl.odtl_goods_type and goods2.goodsid=odtl_goods_id

where order_id=1

參考

根據字段值查詢其所在的表 字段

假如字段值 123456,根據其查詢表名和欄位名 declare what varchar 800 set what 123456 要搜尋的字串 declare sql varchar 8000 declare tablecursor cursor local for select sql if e...

根據表名獲取表字段資訊

實際應用中不免需要調取資料庫表字段的相關資訊,特此將 貼出,以作備用,調取的資訊 欄位名 字段型別 字段長度 是否主鍵 說明 建立sqlparameter public class cp public sqldbtype coltype public int collength public boo...

根據字段值取字段別名

描述 根據某一字段的不同值,為另一字段取不同的別名。背景 建乙個學生缺勤表,其中有乙個欄位是缺勤狀態status,取值為0 5,分別代表曠課 早到 遲到 病假 事假 公假。求出各系 各缺勤狀態的人數。select 系別,sum case when 缺勤狀態 0 then 1 else 0 end a...