1

我有两张桌子:一张叫“Collected_items”,另一张叫“Wrong_Collected”。如下所示:

**collected_items**
item_no qty
x1       10
x2       20
BB       5
Z1       20
x13      13


**wrong_collected**
item_no qty
x1      10
x2      20
x13     13

如您所见,两个表上都出现了项目编号,但我只想输出在 wrong_collected 表中找不到的项目编号。

$sql = mysql_query("SELECT collected_items.item_no,wrong_collected.item_no FROM collected_items, wrong_collected WHERE collected_items.item_no!=wrong_collected.item_no");
while($data = mysql_fetch_array($sql)){
   echo $data["item_no"];
}

输出显示所有项目编号,除了BBZ1..Althout 如果您!collected_items.item_no=wrong_collected.item_no输出中删除,将显示匹配的 item_no,如下所示:

X1 X2 X13

抱歉,我对 PHP 不是很熟悉。我已经学习了 JOIN,但我从未见过有关如何输出不匹配结果的示例。请帮忙 !

4

3 回答 3

0

这是subquery给出不匹配结果的示例

SELECT item_no FROM collected_items WHERE item_no  NOT IN 
(SELECT item_no  FROM  wrong_collected)

希望有意义

于 2013-06-16T11:38:10.953 回答
0

改变这个:

SELECT collected_items.item_no,wrong_collected.item_no FROM collected_items, wrong_collected WHERE collected_items.item_no!=wrong_collected.item_no

至:

SELECT ci.item_no FROM collected_items ci 
left join wrong_collected wc on ci.item_no=wc.item_no 
WHERE wc.item_no is null;
于 2013-06-16T11:26:47.230 回答
0

使用这个查询:

 select * from collected_items ci where not exists (select 1 from wrong_collected wc where ci.item_no = wc.item_no)
于 2013-06-16T11:33:24.323 回答