2014-05-17 51 views
0

在我的站點的其中一個頁面上,我在4個表(登錄名,統計信息,輪詢和通告)上使用內部聯接,如下所示:四個表的三個內部聯接,一些值可能在查詢中可能爲空或者可能不爲空

$login_user = mysqli_real_escape_string($cxn, $_SESSION['user']); 

$sqlAll = mysqli_fetch_assoc(mysqli_query($cxn, 
    "SELECT login.picture, login.statement, 
    stats.wealth, 
    MAX(poll.polltime) AS pollunixtime, 
    MAX(announce.announcetime) AS announceunixtime 
    FROM login 
    INNER JOIN stats ON login.user = stats.user 
    INNER JOIN poll ON login.user = poll.user 
    INNER JOIN announce ON login.user = announce.source 
    WHERE (login.user = '$login_user' AND announce.announcetype = 'feedback')")); 

該內部聯接工作正常,只要在表格至少一個行宣佈,並在表調查填充該用戶,例如,這種形式輸入的值定義一行:

<form id="myForm"><input type="hidden" name="wealth" value="<?= $sqlAll['wealth'] ?>"></form>//"View Page Source" shows this value is defined and shows up as value="number" 

H不過,我注意到,當用戶的表中沒有任何行存在時,查詢失敗,因爲查詢中的某些值未定義。例如,如果沒有行對於任何一個表,這個表格輸入的值是不確定的:

<form id="myForm"><input type="hidden" name="wealth" value="<?= $sqlAll['wealth'] ?>"></form>//"View Page Source" shows this value is undefined and shows up as value="" 

我試圖引入CASE到我的查詢以各種不同的方式,例如:

$sqlAll = mysqli_fetch_assoc(mysqli_query($cxn, 
    "SELECT login.picture, login.statement, 
    stats.wealth, 
    MAX(poll.polltime) AS pollunixtime, 
    MAX(announce.announcetime) AS announceunixtime 
    FROM login 
    INNER JOIN stats ON login.user = stats.user 
    INNER JOIN poll ON CASE WHEN poll.polltime IS NOT NULL THEN login.user = poll.user END 
    INNER JOIN announce ON CASE WHEN announce.announcetime IS NOT NULL THEN login.user = announce.source END 
    WHERE (login.user = '$login_user' AND announce.announcetype = 'feedback')")); 

由於的pollTime和announcetime是INT類型的,我還試圖用「>」,如下所示:

$sqlAll = mysqli_fetch_assoc(mysqli_query($cxn, 
    "SELECT login.picture, login.statement, 
    stats.wealth, 
    MAX(poll.polltime) AS pollunixtime, 
    MAX(announce.announcetime) AS announceunixtime 
    FROM login 
    INNER JOIN stats ON login.user = stats.user 
    INNER JOIN poll ON CASE WHEN poll.polltime > 0 THEN login.user = poll.user END  
    INNER JOIN announce ON CASE WHEN announce.announcetime > 0 THEN login.user = announce.source END 
    WHERE (login.user = '$login_user' AND announce.announcetype = 'feedback')")); 

我也試圖變型中CASE語句置於與輪詢宣佈如下:

$sqlAll = mysqli_fetch_assoc(mysqli_query($cxn, 
    "SELECT login.picture, login.statement, 
    stats.wealth, 
    CASE WHEN MAX(poll.polltime) IS NOT NULL THEN 'pollunixtime' END, 
    CASE WHEN MAX(announce.announcetime) IS NOT NULL THEN 'announceunixtime' END 
    FROM login 
    INNER JOIN stats ON login.user = stats.user 
    INNER JOIN poll ON login.user = poll.user 
    INNER JOIN announce ON login.user = announce.source 
    WHERE (login.user = '$login_user' AND announce.announcetype = 'feedback')")); 

沒有變化我都試過了工作。基本上,我需要一個CONDITIONAL INNER JOIN,以便在給定用戶存在polltime和announcetime的情況下執行第二個和第三個內部聯接,否則不執行第二個和第三個內部聯接。這可能嗎?

請注意,我已經有一個hack解決方案,它在這個系統中涉及將上面的一個查詢分解成3個單獨的查詢,並使用ISSET來測試給定用戶是否存在polltime和announcetime。我想比這個解決方案做得更好!

回答

1

您可以使用LEFT JOIN而不是INNER JOIN來做一些與您要求的內容有點相似的內容。

+0

但是LEFT JOIN是否佔空值? –

+0

假設我們改變了你的查詢來說'LEFT JOIN stats ON login.user = stats.user LEFT JOIN poll ON login.user = poll.user LEFT JOIN announce ON login.user = announce.source'。這意味着你的「登錄」行將全部存在,無論「stats」,「poll」還是「announce」中存在相應的行。這聽起來像你想要做的? –

+0

是的......好吧,我會試試這個 –

相關問題