你可以left join
用来获取你需要的所有数据
文档 在这里
您的查询有误
"SELECT * FROM `products` p LEFT JOIN `materialsproducts` mp on p.`id` = mp.`productid` LEFT JOIN `materials` m ON m.`id` = mp.`materialid` WHERE $whateveryouneed"
请记住不要将您限制WHERE
为只有 1 个,ID
否则您将需要更多查询。
UPDATED
如被问及
products has a 1-* relationship with both materialsproducts and typesproducts. materialsproducts has a *-1 relationship with materials. typesproducts has a *-1 relationship with types.
所以你可以通过left join
这种方式进行如上的查询
SELECT * FROM `products` p
LEFT JOIN `materialsproducts` mp
ON p.`id` = mp.`productid`
LEFT JOIN `materials` m
ON mp.`mp_field_id_here` = m.`m_field_id_here` //here you need to change with actual field to compare
LEFT JOIN `typesproducts` tp
ON p.`id` = tp.`tp_field_id_here` //here you need to change with actual field to compare
LEFT JOIN `types` t
ON tp.`tp_field_id_here` = t.`t_field_id_here` //here you need to change with actual field to compare
然后您可以添加一个where
语句以将结果限制为适合您的内容
WHERE WHATEVER_YOU_NEED
UPDATED AGAIN
将结果限制为仅提交一些文件,只需更改*
为特定的内容
"SELECT p.`Product_Name`, m.`Material_1`, m.`Material_2, m.`Material_3`, t.`Type_1`, t.`Type_2` FROM .....
注意我假设您material 1, material 2, material 3
从material
表中检索这就是我用作前缀的原因m
。无论哪种方式,我都用作前缀t
,type 1 and type 2
因为我认为它们是type
表格的文件,否则您可以根据需要更改它们。