mysql在两个表中排除具有相同id的结果

时间:2015-08-01 18:54:00

标签: mysql distinct

我有一个可以取得一些结果的工作查询。

    $sql = "
        SELECT 
            swdl.wedding_dress AS wedding_dress,
            wd.name AS name,
            wdi.url AS image
        FROM salons_wedding_dresses_link AS swdl 
            LEFT JOIN wedding_dresses AS wd ON swdl.wedding_dress = wd.id
            LEFT JOIN wedding_dress_images AS wdi ON wdi.wedding_dress = wd.id AND wdi.main_image = 1    
            WHERE swdl.salon = ? AND wd.active = 1
    ";

我想要的是将其更新为只获取表wedding_dress中的salons_wedding_dresses_link不在wedding_dress_dress_collection_link列中的wedding_dress_id表中但我没有这样做的结果。 enter image description here Bellow是更新的查询,更新为粗体。

            $sql = "
        SELECT 
            swdl.wedding_dress AS wedding_dress,
            wd.name AS name,
            wdi.url AS image
        FROM salons_wedding_dresses_link AS swdl 


LEFT JOIN wedding_dress_dress_collection_link AS wddcl
------------------------------------------------------

            LEFT JOIN wedding_dresses AS wd ON swdl.wedding_dress = wd.id
            LEFT JOIN wedding_dress_images AS wdi ON wdi.wedding_dress = wd.id AND wdi.main_image = 1    
            WHERE 

swdl.wedding_dress NOT IN (SELECT wddcl.wedding_dress_id FROM wddcl) AND
------------------------------------------------------------------------



swdl.salon = ? AND wd.active = 1
        ";

1 个答案:

答案 0 :(得分:0)

选择查询以获取salons_wedding_dresses_link但不在wedding_dress_dress_collection_link中的结果:

 select s1.*,s2.wedding_dress_collection_id,s2.wedding_dress_id  
 from salons_wedding_dresses_link s1
 left join wedding_dress_dress_collection_link s2 on
 s1.wedding_dress=s2.wedding_dress_id 
 whrere s2.wedding_dress_id  is null;