我有一些mysql查询的问题。 我试图用每个属性的列中的每个属性构建包含所有产品ID,产品名称,制造商名称和数量的表格 获取ID和名称很容易。当我尝试为每个属性加入列并用数量填充时,问题就开始了。某些属性未与产品相关,因此ps_stock_available中没有id_stock_available(数量) 在PHP代码中我从db到数组获取attibutes id。比我基于该数组构建查询。 我不会将整个查询粘贴为465行(包括行之间的空格)。将粘贴开始,中间查询的一部分和。 如果我只使用$ i = 1(t1.id_attribute1)
运行查询SELECT
ps_2product_lang.id_product,
ps_2product_lang.name,
ps_2category_lang.name AS manufacturer_name,
t1.id_attribute1
FROM ps_2product_lang
LEFT JOIN ps_2product
ON ps_2product_lang.id_product = ps_2product.id_product
LEFT JOIN ps_2category_lang
ON ps_2product.id_category_default = ps_2category_lang.id_category
LEFT JOIN ps_2category
ON ps_2category_lang.id_category = ps_2category.id_category
JOIN
(
SELECT
ps_2stock_available.id_product AS id_product,
ps_2stock_available.quantity AS id_attribute1
FROM ps_2stock_available
JOIN
ps_2product_attribute_combination
ON
ps_2stock_available.id_product_attribute = ps_2product_attribute_combination.id_product_attribute
WHERE
ps_2product_attribute_combination.id_attribute = 1
ORDER BY
id_product
)
AS t1
ON
ps_2product_lang.id_product = t1.id_product
WHERE ps_2product_lang.id_lang = 7
AND ps_2category_lang.id_lang = 7
AND ps_2product_lang.id_product > 12
AND (ps_2category.id_parent = 18 OR ps_2category.id_parent = 84)
ORDER BY
manufacturer_name, name
我得到的产品id_attribute = 1,其中column attribute1填充了数据(数量) 下面的部分在查询中重复多次,因为在prestashop中创建了属性。
JOIN
(
SELECT
ps_2stock_available.id_product AS id_product,
ps_2stock_available.quantity AS id_attribute1
FROM ps_2stock_available
JOIN
ps_2product_attribute_combination
ON
ps_2stock_available.id_product_attribute = ps_2product_attribute_combination.id_product_attribute
WHERE
ps_2product_attribute_combination.id_attribute = 1
ORDER BY
id_product
)
AS t1
ON
ps_2product_lang.id_product = t1.id_product
现在查询中的JOIN越多,我从DB获得的结果就越少,好像JOIN像AND一样工作。
正如我所提到的,一些属性没有分配给产品,因此没有数量。在那些想要填充em with NULL value but i havent included that in query (i thought that JOIN will return null
以查询结果的id中)
我究竟做错了什么?
非常感谢提前