在Left Join上只将一个表添加WHERE子句?

时间:2011-10-04 01:59:22

标签: mysql

我正在Sites加入History,并选择不在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

这可能吗?我怎么能这样做?

3 个答案:

答案 0 :(得分: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

注意:您应在此前面加上相应的表名称。我会,但你没有说明。

答案 1 :(得分:1)

是这样的:

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

答案 2 :(得分:0)

尝试使用not exists子句:

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