0

这是大 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`)
)
4

0 回答 0