I have a query that would get all product names and concatenate the names to 1 column. But I can't figure out how to make it work. Here is the query.
SELECT package_id,
GROUP_CONCAT
(
SELECT c.product_name FROM tbl_product c WHERE c.product_id IN
(
SELECT a.product_id FROM tbl_package_detail a WHERE a.package_id = b.package_id
)
ORDER BY c.product_id
)
FROM tbl_package b
GROUP BY package_id
Note that:
SELECT c.product_name FROM tbl_product c WHERE c.product_id IN
(
SELECT a.product_id FROM tbl_package_detail a WHERE a.package_id = b.package_id
)
ORDER BY c.product_id
Returns all the product names I need. I am not sure if I am using GROUP_CONCAT
correctly.
ERROR MESSAGE:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' SELECT c.product_name FROM tbl_product c WHERE c.product_id IN
'
Try the following
SELECT
b.package_id,
GROUP_CONCAT(c.product_name ORDER BY c.product_id) ProductList
FROM tbl_package b
LEFT JOIN tbl_package_detail a ON a.package_id = b.package_id
LEFT JOIN tbl_product c ON c.product_id=a.product_id
GROUP BY b.package_id
GROUP_CONCAT
is an aggregate function like SUM/AVG/MIN/MAX/COUNT
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.