donglie9067 2013-06-16 11:23
浏览 52
已采纳

PHP MYSQL加入并输出无与伦比的结果

I've two tables : One called "Collected_items" and the Other "Wrong_Collected"..As shown below :

**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

As you can see, there are item no.'s occurred on both tables, but all i want is to output the one's which are not found in wrong_collected table.

$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"];
}

The output shows all item no.'s except BB and Z1 ..Althout if you remove the ! from collected_items.item_no=wrong_collected.item_no the output will show the matching item_no's as follows :

X1 X2 X13

Sorry, I'm not really familiar with PHP. I've learned JOIN, but i never saw an example on how to output the unmatched results. Please Help !

  • 写回答

3条回答 默认 最新

  • doujiao7325 2013-06-16 11:26
    关注

    change this:

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

    to:

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?