left join使0结果而不是空行

时间:2013-09-05 15:16:51

标签: mysql

这是大sql查询,它可以正常工作

select * from (SELECT DISTINCT `t`.*
FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_podsrel` AS `rel_city` 
    ON ( `rel_city`.`field_id` = 13918 AND `rel_city`.`item_id` = `t`.`id` ) 
LEFT JOIN `wp_pods_city` AS `city` 
    ON ( `rel_city`.`field_id` = 13918 AND `city`.`id` = `rel_city`.`related_item_id` ) 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_what` 
    ON ( `rel_goodfor_what`.`field_id` = 13912 AND `rel_goodfor_what`.`item_id` = `t`.`id` ) 
LEFT JOIN `wp_pods_goodfor` AS `goodfor_what` 
    ON ( `rel_goodfor_what`.`field_id` = 13912 AND `goodfor_what`.`id` = `rel_goodfor_what`.`related_item_id` ) 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_who` 
    ON `rel_goodfor_who`.`field_id` = 13911 AND `rel_goodfor_who`.`item_id` = `t`.`id` 
LEFT JOIN `wp_pods_goodfor_who` AS `goodfor_who` 
    ON `rel_goodfor_who`.`field_id` = 13911 AND `goodfor_who`.`id` = `rel_goodfor_who`.`related_item_id` 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_when` 
    ON `rel_goodfor_when`.`field_id` = 13913 AND `rel_goodfor_when`.`item_id` = `t`.`id` 
LEFT JOIN `wp_pods_goodfor_when` AS `goodfor_when` ON `rel_goodfor_when`.`field_id` = 13913 
    AND `goodfor_when`.`id` = `rel_goodfor_when`.`related_item_id`  
    WHERE  hide=0 AND opening_soon=0 AND ( `city`.`name` = "London" )  AND `goodfor_when`.`name` IN ('Late') GROUP BY name HAVING COUNT(DISTINCT `goodfor_when`.`name`) = 1
  ORDER BY `t`.`name`, `t`.`id`
                ) as foo

LEFT JOIN  `wp_podsrel` AS `pr`
  ON `field_id` = 13905 AND `item_id` = foo.id

并返回144个结果。现在我再添加一个left join而没有结果(见底部)

select * from (SELECT DISTINCT `t`.*
FROM `wp_pods_bars` AS `t` LEFT JOIN `wp_podsrel` AS `rel_city` 
    ON ( `rel_city`.`field_id` = 13918 AND `rel_city`.`item_id` = `t`.`id` ) 
LEFT JOIN `wp_pods_city` AS `city` 
    ON ( `rel_city`.`field_id` = 13918 AND `city`.`id` = `rel_city`.`related_item_id` ) 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_what` 
    ON ( `rel_goodfor_what`.`field_id` = 13912 AND `rel_goodfor_what`.`item_id` = `t`.`id` ) 
LEFT JOIN `wp_pods_goodfor` AS `goodfor_what` 
    ON ( `rel_goodfor_what`.`field_id` = 13912 AND `goodfor_what`.`id` = `rel_goodfor_what`.`related_item_id` ) 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_who` 
    ON `rel_goodfor_who`.`field_id` = 13911 AND `rel_goodfor_who`.`item_id` = `t`.`id` 
LEFT JOIN `wp_pods_goodfor_who` AS `goodfor_who` 
    ON `rel_goodfor_who`.`field_id` = 13911 AND `goodfor_who`.`id` = `rel_goodfor_who`.`related_item_id` 
LEFT JOIN `wp_podsrel` AS `rel_goodfor_when` 
    ON `rel_goodfor_when`.`field_id` = 13913 AND `rel_goodfor_when`.`item_id` = `t`.`id` 
LEFT JOIN `wp_pods_goodfor_when` AS `goodfor_when` ON `rel_goodfor_when`.`field_id` = 13913 
    AND `goodfor_when`.`id` = `rel_goodfor_when`.`related_item_id`  
    WHERE  hide=0 AND opening_soon=0 AND ( `city`.`name` = "London" )  AND `goodfor_when`.`name` IN ('Late') GROUP BY name HAVING COUNT(DISTINCT `goodfor_when`.`name`) = 1
  ORDER BY `t`.`name`, `t`.`id`
                ) as foo

LEFT JOIN  `wp_podsrel` AS `pr`
  ON `field_id` = 13905 AND `item_id` = foo.id
LEFT JOIN  `wp_posts` 
    ON `pr`.related_item_id = `wp_posts`.`ID`  

所以现在这个查询返回0个结果。如果找不到匹配,我想它应该只添加空行?这是wp_podsrel结构

`wp_podsrel` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `pod_id` int(10) unsigned DEFAULT NULL,
  `field_id` int(10) unsigned DEFAULT NULL,
  `item_id` bigint(20) unsigned DEFAULT NULL,
  `related_pod_id` int(10) unsigned DEFAULT NULL,
  `related_field_id` int(10) unsigned DEFAULT NULL,
  `related_item_id` bigint(20) unsigned DEFAULT NULL,
  `weight` smallint(5) unsigned DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `field_item_idx` (`field_id`,`item_id`),
  KEY `rel_field_rel_item_idx` (`related_field_id`,`related_item_id`),
  KEY `field_rel_item_idx` (`field_id`,`related_item_id`),
  KEY `rel_field_item_idx` (`related_field_id`,`item_id`)
)

0 个答案:

没有答案