mysql获得完全连接的相反结果

时间:2012-11-06 23:53:46

标签: mysql join

此查询为我提供了我想要的结果,并且效果很好。

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

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

3 个答案:

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

答案 1 :(得分:1)

确保您在datemerchnumber上拥有多字段索引,以提高您的速度。

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

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

此致