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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程