簡體   English   中英

將WHERE子句添加到Left Join的僅一個表中?

[英]Add WHERE clause to just one table on Left Join?

我要加入具有“ History Sites ,並選擇“ History中沒有的行。

$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

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

您可以將其添加到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

注意:您應該為此加上適當的表名作為前綴。 我可以,但您沒有指定。

是這樣的:

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

嘗試使用not exists子句:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM