2012-06-19 44 views
1

以下查詢返回bumps表中同時具有loginidip2的行。查詢結果是否包含SELECT字段

bumps表中的所有行都有ip2,但只有一些有loginid

如何獲得下面的查詢以便返回ip2所有行和loginid對於具有loginid的行?

$sqlStrend = "SELECT e.loginid, e.time, l.username, e.ip2 
     FROM bumps e 
     JOIN login l ON e.loginid = l.loginid 
    WHERE e.submissionid = '$submissionid' 
    ORDER BY e.time DESC 
    LIMIT $offset, $rowsperpage"; 

回答

3

不是一個INNER JOIN(由JOIN隱含的),你只需要將此更改爲LEFT JOIN,返回所有行的左側表和那些相關的或NULL其中相關的錶行不存在。

SELECT e.loginid, e.time, l.username, e.ip2 
FROM bumps e 
LEFT JOIN login l ON e.loginid = l.loginid 
WHERE e.submissionid = '$submissionid' 
ORDER BY e.time DESC 
LIMIT $offset, $rowsperpage 

審查不同JOIN類型this excellent Wikipedia articleJeff Atwood's Visual Explanation of Joins

1

實際上,你實際上是大多數情況。要獲得所有從顛簸行和行登錄存在,你想要一個LEFT JOIN

$sqlStrend = "SELECT e.loginid, e.time, l.username, e.ip2 
     FROM bumps e 
     LEFT JOIN login l ON e.loginid = l.loginid 
    WHERE e.submissionid = '$submissionid' 
    ORDER BY e.time DESC 
    LIMIT $offset, $rowsperpage";