作者: carlkyo 時間: 2017-10-23 16:21 標題: [請教] mysql join search
- SELECT a.id,a.typeID,b.type,c.email_ok,d.esys_ok,e.reason FROM `cp_apply` a
- LEFT JOIN cp_type b ON(a.typeID = b.id)
- LEFT JOIN cp_email c ON(a.id = c.apply_id)
- LEFT JOIN cp_esys d ON(a.id = d.apply_id)
- LEFT JOIN cp_reject e ON(a.id = e.apply_id)
- WHERE a.typeID BETWEEN 3 AND 5
- AND a.centerID = 32
- AND e.reason IS NULL
我以上的join出到以下既資料
[attach]2019273[/attach]
- SELECT a.id,a.typeID,b.type,c.email_ok,d.esys_ok,e.reason FROM `cp_apply` a
- LEFT JOIN cp_type b ON(a.typeID = b.id)
- LEFT JOIN cp_email c ON(a.id = c.apply_id)
- LEFT JOIN cp_esys d ON(a.id = d.apply_id)
- LEFT JOIN cp_reject e ON(a.id = e.apply_id)
- WHERE a.typeID BETWEEN 3 AND 5
- AND a.centerID = 32
- AND e.reason IS NULL
- AND c.email_ok != 2
- AND d.esys_ok != 2
改極都唔得
係唔係我本身既join錯左?
many thanks
作者: faiwaic 時間: 2017-10-23 16:49
請教下各位ching.
我以上的join出到以下既資料
但我唔想出email_ok & esys_ok 都係2既資料
改極都唔得
係唔 ...
carlkyo 發表於 2017-10-23 16:21
你唔係 join 錯, 係唔好 select 出黎就得....好 basic 野....(紅字 delete)
SELECT a.id,a.typeID,b.type,c.email_ok,d.esys_ok,e.reason FROM `cp_apply` a
LEFT JOIN cp_type b ON(a.typeID = b.id)
LEFT JOIN cp_email c ON(a.id = c.apply_id)
LEFT JOIN cp_esys d ON(a.id = d.apply_id)
LEFT JOIN cp_reject e ON(a.id = e.apply_id)
WHERE a.typeID BETWEEN 3 AND 5
AND a.centerID = 32
AND e.reason IS NULL
AND c.email_ok != 2
AND d.esys_ok != 2
作者: carlkyo 時間: 2017-10-23 19:42
回覆 2# faiwaic
- SELECT a.id,a.typeID,b.type,e.reason FROM `cp_apply` a
- LEFT JOIN cp_type b ON(a.typeID = b.id)
- LEFT JOIN cp_email c ON(a.id = c.apply_id)
- LEFT JOIN cp_esys d ON(a.id = d.apply_id)
- LEFT JOIN cp_reject e ON(a.id = e.apply_id)
- WHERE a.typeID BETWEEN 3 AND 5
- AND a.centerID = 32
- AND e.reason IS NULL
- AND c.email_ok != 2
- AND d.esys_ok != 2
- 沒有行。
作者: carlkyo 時間: 2017-10-23 20:46
本帖最後由 carlkyo 於 2017-10-23 20:47 編輯
- SELECT a.id,a.centerID,
- b.center_name,
- c.email_ok,
- d.esys_ok,
- e.reason
- FROM cp_apply a
- INNER JOIN cp_center b ON a.centerID = b.id
- LEFT JOIN cp_email c ON a.id = c.apply_id
- LEFT JOIN cp_esys d ON a.id = d.apply_id
- LEFT JOIN cp_reject e ON a.id = e.apply_id
- where c.email_ok != 2 and d.esys_ok != 2
id centerID center_name email_ok esys_ok reason
105 43 其他 1 1 NULL
作者: carlkyo 時間: 2017-10-23 21:07
本帖最後由 carlkyo 於 2017-10-23 21:24 編輯
- SELECT t1.*,
- t2.center_name,
- t3.email_ok,
- t4.esys_ok,
- t5.reason
- FROM cp_apply t1
- JOIN cp_center t2 ON t1.centerID = t2.id
- left JOIN cp_email t3 ON t1.id = t3.apply_id
- left JOIN cp_esys t4 ON t1.id = t4.apply_id
- left JOIN cp_reject t5 ON t1.id = t5.apply_id
- where
- t3.email_ok is null and t4.esys_ok is null
- and
- t5.reason is NULL
- and
- t1.typeID BETWEEN 1 AND 2
- and
- t1.centerID = 32
- or
- t3.email_ok != 2 or t4.esys_ok != 2
- and
- t5.reason is NULL
- and
- t1.typeID BETWEEN 1 AND 2
- and
- t1.centerID = 32
many thanks
作者: faiwaic 時間: 2017-10-24 09:25
回覆 5# carlkyo
SELECT t1.*,
t2.center_name,
t3.email_ok,
t4.esys_ok,
t5.reason
FROM cp_apply t1
JOIN cp_center t2 ON t1.centerID = t2.id
left JOIN cp_email t3 ON t1.id = t3.apply_id
left JOIN cp_esys t4 ON t1.id = t4.apply_id
left JOIN cp_reject t5 ON t1.id = t5.apply_id
where
t3.email_ok is null and t4.esys_ok is null
and
t5.reason is NULL
and
t1.typeID BETWEEN 1 AND 2
and
t1.centerID = 32
or
t3.email_ok != 2 or t4.esys_ok != 2
and
t5.reason is NULL
and
t1.typeID BETWEEN 1 AND 2
and
t1.centerID = 32
你唔想 select (show) 出黎 同 個 value 本身係 吉 (null) 係兩回事喎 ching~
作者: carlkyo 時間: 2017-10-24 20:38
回覆 6# faiwaic
我想搵t5.reason係null,t1.typeID BETWEEN 1 AND 2
t1.centerID = 32
t3.email_ok同t4.esys_ok都唔係2既資料
many thanks
作者: faiwaic 時間: 2017-10-25 10:30
回覆 7# carlkyo
- SELECT t1.*,t2.center_name,t3.email_ok,t4.esys_ok,t5.reason
- FROM cp_apply t1
- JOIN cp_center t2 ON t1.centerID = t2.id
- left JOIN cp_email t3 ON t1.id = t3.apply_id
- left JOIN cp_esys t4 ON t1.id = t4.apply_id
- left JOIN cp_reject t5 ON t1.id = t5.apply_id
- where
- t5.reason is NULL
- and t1.typeID BETWEEN 1 AND 2
- and t1.centerID = 32
- and t3.email_ok <> 2
- and t4.esys_ok <> 2
以你所求應該係咁咋喎~
作者: 紫河馬 時間: 2017-10-25 12:00
本帖最後由 紫河馬 於 2017-10-25 12:05 編輯
left join table (cp_email,cp_esys, cp_reject ) 既filter唔好放去where,
萬一cp_apply 有d record join唔到left table, 會因為where clause 而filter走(即係變左inner join...)
作者: carlkyo 時間: 2017-10-25 15:25
left join table (cp_email,cp_esys, cp_reject ) 既filter唔好放去where,
萬一cp_apply 有d record join ...
紫河馬 發表於 2017-10-25 12:00
cp_email,cp_esys, cp_reject 本身係冇record
我唔想顯示cp_apply join完cp_email,cp_esys 任何一個等於2既資料
many thanks

