这个查询给了我想要的结果,而且效果很好。
SELECT *
FROM npc5
FULL JOIN joeall ON merchnumber = joeall.id
WHERE date='$period'
ORDER BY DATE");
现在我想获取商家编号不在joeall
表中的记录,例如ON merchnumber != joeall.id
排除在 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");
确保你有一个多字段索引date
并merchnumber
保持你的速度。
SELECT *
FROM npc5
WHERE date='$period'
AND merchnumber NOT IN (SELECT id FROM JOEALL)
ORDER BY DATE
相关子查询可能非常低效,应尽可能避免。
因此我不建议你这样做
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;
问候