Mysql查询多个表检索所有结果甚至为null

时间:2013-06-15 19:07:43

标签: mysql join

我尝试使此查询包含所有products_options_name结果,即使信息表中没有所选语言的相关行也是如此。 当我现在运行查询时,只显示products_propoptions,其中包含信息表中所选语言的相关行。

非常感谢任何帮助。 提前致谢, BAS

SELECT PPO.products_options_name, I.information_title, PTL.information_id, 
COUNT(PPA.products_attributes_id) as amount, 
options_id FROM products_propattributes PPA 
INNER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id
LEFT JOIN properties_to_information PTL ON PPA.options_id = PTL.option_id   
LEFT JOIN information I ON PTL.information_id = I.information_id
WHERE PPO.language_id = 6 AND I.language_id = 6 AND PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order

表:products_propoptions

  • products_options_id
  • language_id
  • products_options_name
  • products_options_sort_order

表:products_propattributes

  • products_attributes_id
  • products_id
  • options_id
  • options_values_id
  • options_values_price
  • price_prefix
  • products_options_sort_order

表:信息

  • information_id
  • information_group_id
  • information_title
  • information_description
  • PARENT_ID
  • SORT_ORDER
  • 可见
  • NOINDEX
  • LANGUAGE_ID

表:properties_to_information

  • properties_to_information_id
  • option_id
  • information_id

2 个答案:

答案 0 :(得分:0)

您是否尝试将INNER JOIN更改为LEFT JOIN? INNER JOIN可能导致查询省略您正在寻找的结果集行。

答案 1 :(得分:0)

这修好了.. 包含LEFT OUTER JOINS以及连接中的where条件... 谢谢.. BAS

SELECT 
PPO.products_options_name,
PTL.lexicon_id, 
I.information_title, 
COUNT(PPA.products_attributes_id) as amount, 
options_id 
FROM products_propattributes PPA  
LEFT OUTER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id AND PPO.language_id = 6
LEFT OUTER JOIN  properties_to_information PTL ON PPA.options_id = PTL.option_id   
LEFT OUTER JOIN information I ON PTL.lexicon_id = I.information_id  AND I.language_id = 6
WHERE PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order