2011-10-04 68 views
2

我加入SitesHistory並選擇不在History中的行。將WHERE子句添加到Left Join上的一個表中?

$data = mysql_query(" 
     select * from Sites 
     left join History 
     on Sites.URL = History.URL 
     where History.URL is null 
     order by Karma 
     "); 

的問題是,我想補充WHERE UID = $uid錶的歷史,所以我只參加行與所述UID。

我想要做這樣的事情:

select * from Sites 
    left join History where UID = $uid 
    on Sites.URL = History.URL 
    where History.URL is null 
    order by Karma 

這可能嗎?我怎樣才能做到這一點?

回答

3

您可以將它添加到JOIN條款:

select * from Sites 
left join History ON UID = $uid 
AND Sites.URL = History.URL 
where History.URL is null 
order by Karma 

WHERE條款:

select * from Sites 
left join History 
AND Sites.URL = History.URL 
where History.URL is null 
and UID = $uid 
order by Karma 

注意:您應該用適當的表名加前綴。我會但你沒有指定。

0

嘗試用not exists條款:

select * from Sites 
where not exists (
    select * from History 
    where UID = $uid 
    and Sites.URL = History.URL) 
order by Karma 
1

是這樣說的:

select * from Sites s 
Where Not Exists 
    (Select * From History 
    Where URL Is Null 
     And uid = $uid) 
order by Karma