查询代码
SELECT * FROM products
LEFT JOIN (SELECT * FROM product_images ) as images ON products.id = images.productId
LEFT JOIN (SELECT * FROM product_shippings ) as shipping ON products.id = shipping.productId
WHERE products.id = :productID
product_images中有多张图像,但是它只返回一个实例。
我想做的是将它们全部存储在一个关联数组中,例如包含图像列表的图像,包含装运项目列表的装运等。
我知道如何使用单独的查询来实现这一点,但是根据我的基准测试,这太慢了。
所以我想要类似
{
images:[...],
shipping[...],
productId:2,
productTitle:'Hello'
}
如何解决此问题?
答案 0 :(得分:1)
SELECT *
FROM products
LEFT JOIN product_images as images ON products.id = images.productId
LEFT JOIN product_shippings as shipping ON products.id = shipping.productId
WHERE products.id = :productID
或者,为了得到逗号(而不是“数组”)
SELECT p.id, p.title,
GROUP_CONCAT(i.image) AS images,
GROUP_CONCAT(s.shipping) AS shippings
FROM products AS p
LEFT JOIN product_images AS i ON p.id = i.productId
LEFT JOIN product_shippings AS s ON p.id = s.productId
WHERE p.id = :productID
答案 1 :(得分:0)
如果您拿走GROUP BY,您将在WHERE子句中获得product.id的所有图像。