2016-12-06 82 views
0

這是我的MySQL數據庫中的註冊表。如果不存在零值,則只會返回SQL記錄

SIGNUP 
id (int) 
customer (int) 
helper (int) 
eid (int) 
..plus more fields 

當我想讓它返回數據庫中的記錄時,此查詢返回0結果。情況是,SIGNUP表有一個記錄,客戶= 11,幫助= 0,批准= 0

如果助手字段填充的數字不是0,那麼記錄會返回,但如果它是0,那麼那裏是0條記錄返回。如果我拿出第二條選擇線(包含v.field1,v.field2等),那麼它也可以工作。問題似乎是沒有數據要從幫助列中提取,因此它不允許返回任何其他內容,但仍有其他字段要返回的數據。打開想法和所有幫助表示讚賞!非常感謝。

SELECT 
    u.id cId, u.first cFirst, u.email cEmail, u.username cUsername, 
    v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, 
    s.customer, s.helper, s.eid EID, s.approved, 
    e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip 
FROM signup s 
INNER JOIN events e ON e.id = s.eid 
INNER JOIN users u ON u.id = s.customer 
INNER JOIN users v ON v.id = s.helper 
WHERE 
    (s.customer = 11 OR s.helper = 11) 
AND e.date > sysdate() 
ORDER BY e.date asc 
+0

嗨,這是正確的連接? INNER JOIN events e ON e.id = s.eid INNER JOIN用戶u ON u.id = s.customer INNER JOIN用戶v ON v.id = s.helper,因爲您通常需要通過id和外鍵進行綁定分別爲 – jam

回答

1

試試這個:

SELECT 
    u.id cId, u.first cFirst, u.email cEmail, u.username cUsername, 
    v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, 
    s.customer, s.helper, s.eid EID, s.approved, 
    e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip 
FROM signup s 
INNER JOIN events e ON e.id = s.eid 
INNER JOIN users u ON u.id = s.customer 
left JOIN users v ON v.id = s.helper 
WHERE (s.customer = 11 OR s.helper = 11) 
AND e.date > sysdate() 
ORDER BY e.date asc 
+0

謝謝你們左連加固定它 – Retro

1

如果我理解正確,您需要一個LEFT JOIN。這樣,即使沒有匹配的幫助器,您也可以獲得所有行。

SELECT 
    u.id cId, u.first cFirst, u.email cEmail, u.username cUsername, 
    v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, 
    s.customer, s.helper, s.eid EID, s.approved, 
    e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip 
FROM signup s 
INNER JOIN events e ON e.id = s.eid 
INNER JOIN users u ON u.id = s.customer 
LEFT JOIN users v ON v.id = s.helper 
WHERE 
    (s.customer = 11 OR s.helper = 11) 
AND e.date > sysdate() 
ORDER BY e.date asc 
+0

非常感謝,左連接是正確的 – Retro