开发者

Selecting data with multi table SQL statement returns unexpected result

I want to format SQL results, then export to CSV (phpMyAdmin export).

SQL statement:

SELECT
product.name,
featu开发者_运维问答res.text
FROM products as product, product_features as features
WHERE
features.product_id=products.id LIMIT 0,100

Tables strucutre:

Table products:

------------
id | name
------------
24   Baseball
25   Rope

Table product_features:

--------------------------
id | text      | product_id
--------------------------
45   Leather..   24
46   Hardball    24
47   Nylon       25
48   Black       25

Problem: I get:

Baseball Leather
Baseball Hardball
Rope Nylon
Rope Black

I'm having a hard time wrapping my head around what type of solution do with a SQL statement.

Result I'm Looking for:

Baseball, Leather..., Hardball
Basketball, Nylon, Black


You'll need some type of aggregate:

SELECT product.name, GROUP_CONCAT(features.text)
FROM products JOIN product_features ON(products.id = product_features.product_id)
GROUP BY products.id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜