0

这个查询给了我想要的结果,而且效果很好。

        SELECT * 
        FROM npc5 
        FULL JOIN joeall ON merchnumber = joeall.id 
        WHERE date='$period' 
        ORDER BY DATE");

现在我想获取商家编号不在joeall表中的记录,例如ON merchnumber != joeall.id

4

3 回答 3

1

排除在 NOT IN 的 SELECT STATEMENT 中找到的任何 id

$query1 = mysql_query("
    SELECT * 
    FROM npc5 
    WHERE date='$period' 
    AND merchnumber NOT IN (SELECT id FROM joeall)
    ORDER BY DATE");
于 2012-11-07T00:14:04.517 回答
1

确保你有一个多字段索引datemerchnumber保持你的速度。

    SELECT * 
    FROM npc5 
    WHERE date='$period' 
    AND merchnumber NOT IN (SELECT id FROM JOEALL)
    ORDER BY DATE
于 2012-11-07T00:14:36.237 回答
1

相关子查询可能非常低效,应尽可能避免。

因此我不建议你这样做

SELECT * 
FROM npc5 
WHERE date='$period' 
AND merchnumber NOT IN (SELECT id FROM JOEALL)
ORDER BY DATE

看看http://devzone.advantagedatabase.com/dz/content.aspx?Key=42&ID=75

我建议您将其更改为类似

SELECT 
    * 
FROM npc5 LEFT JOIN JOEALL on 
    npc5.merchnumber = joeall.id 
WHERE 
    joeall.id is null 
        and 
    date='$period'
ORDER BY
    date;

问候

于 2012-11-07T00:51:36.003 回答