doqrt26664 2013-06-20 03:35
浏览 12
已采纳

如何制作一个花哨的mysql连接,可以连接三个表并检测一个表是否有我的项目

php mysql query I have multiple linked tables - I also have a table that only creates and entry if certian conditions exist so I would like to add that into my query to avoid having to go through thousands of query searches looking for this special case

here is my current query

$query = "SELECT a.UUID FROM contract a 
        INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
        WHERE b.garcom_UUID = '$garbCom'
        AND b.city_UUID = '$city'";

I then go through each item that was returned (in the thousands)

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
    $sentdata = getothertable($row['UUID']); //checks if the item is in the table
             $sent = $sentdata ['senttoGarcom'];
             if($sent == 0) //if it wasn't found add it to my list
    {
       array_push($Contracts,$row['UUID']);
    }
    }

instead of all that I would like to just make it one query - pseduo code something like this

 $query = "SELECT a.UUID FROM contract a 
            INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
            INNER JOIN contract_sales c ON a.UUID = c.contractUUID
            WHERE b.garcom_UUID = '$garbCom'
            AND b.city_UUID = '$city' AND c.DOESNOTEXIST";

this way I dont have to return thousands I will only be returned the few that are not yet in the contract_sales table and I can go about my business...

Appreciate any help!

  • 写回答

3条回答 默认 最新

  • dtot74529 2013-06-20 03:50
    关注

    just check for NULL rows of c with a outer join

    $query = "SELECT a.UUID FROM contract a 
            INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
            LEFT OUTER JOIN contract_sales c ON a.UUID = c.contractUUID
            WHERE b.garcom_UUID = '$garbCom'
            AND b.city_UUID = '$city' AND c.contractUUID IS NULL ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?