我试图做这样的选择:
SELECT product.*, customers.*, product,id AS productid, (SELECT comment FROM notes WHERE product_id = productid) AS notes FROM products JOIN customers ON customers.product_id = product.id WHERE product.export = 0
它在笔记的子查询中出错,但我不想将它们限制为1我希望获得此产品的无限笔记。这可能吗?如何?或者我是否需要为每个产品进行新查询才能获得笔记?
答案 0 :(得分:1)
查询结果可以看作按行和列划分的单元格网格。每个单元格只能包含一个值。但是你可以做的是使用像GROUP_CONCAT
之类的聚合函数将子查询的所有值组合成单个值。 GROUP_CONCAT
会将所有注释作为单个文本字符串返回。
SELECT product.*, customers.*, product,id AS productid,
(SELECT GROUP_CONCAT(comment) FROM notes WHERE product_id = productid) AS notes
FROM products JOIN customers ON customers.product_id = product.id
WHERE product.export = 0