简体   繁体   English

左连接,其中第一个(左表)具有多个where子句

[英]Left join with multiple where clause for the first (left table)

I got this Query: 我得到这个查询:

$query = mysql_query("SELECT arbejdsopgave.*, 
                     DATE_FORMAT(dato, '%d-%m-%Y') AS tid, 
                     tilfoejelser.*, 
                     DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2
                     FROM arbejdsopgave LEFT JOIN tilfoejelser 
                     ON arbejdsopgave.sub_id=tilfoejelser.sub_id2 
                     WHERE arbejdsopgave.cat_id = '$id' 
                       AND datotf IS NULL 
                       OR datotf = (select max(datotf) 
                         FROM tilfoejelser 
                         WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2)
                         ORDER BY arbejdsopgave.sub_id")
                         or die(mysql_error());

I need to to only show rows from the table " arbejdsopgave " where arbejdsopgave.cat_id = '$id' but also where arbejdsopgave.status = 'closed' 我只需要显示表“ arbejdsopgave ”中的行,其中arbejdsopgave.cat_id = '$id' ,还需要arbejdsopgave.status = 'closed'

I tried both in the join on and in the where. 我尝试了联接和联接。

First I get everything despite the extra where. 首先,尽管有额外的地方,我还是得到了一切。 Secondly I get only the rows where there are a join. 其次,我仅获得有联接的行。

Can anyone help me - I am amazed that i even made it this far looking at my Query... Pls help 任何人都可以帮我吗-令我惊讶的是,我什至在查询时都走了这么远。

This should work: 这应该工作:

SELECT arbejdsopgave.*, DATE_FORMAT(dato, '%d-%m-%Y') AS tid, tilfoejelser.*, DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2 
FROM arbejdsopgave 
LEFT JOIN tilfoejelser ON arbejdsopgave.sub_id=tilfoejelser.sub_id2
WHERE arbejdsopgave.cat_id = '$id' 
AND arbejdsopgave.status = 'closed'
AND (datotf IS NULL 
OR datotf = (
    select max(datotf) FROM tilfoejelser 
    WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2) )
ORDER BY arbejdsopgave.sub_id

I suppose you forget about () around datotf OR condition 我想您会忘记()的datotf OR条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM