简体   繁体   English

将多个MySQL行合并到一个字段中

[英]multiple MySQL rows into one field

I tried a solution in a similar question here at stackoverflow, but with no success. 我在stackoverflow尝试了类似问题的解决方案,但没有成功。 Here is mysql query: 这是mysql查询:

$sql= "SET group_concat_max_len = 2048";
$sql = "select p.id,p.name,p.logo,p.short_description,  GROUP_CONCAT(c.category_name SEPARATOR ', ')  from eshop p, eshop_cat c  GROUP BY p.id";

I can't understand what is wrong with the query, but the c.category is resulting empty. 我无法理解查询出了什么问题,但是c.category结果为空。 If i do this without group concat and use inner join i receive all the data right. 如果我在没有组concat的情况下执行此操作并使用内部连接,则我会正确接收所有数据。 But i have multiple rows for the same product for each category of every product. 但是对于每个产品的每个类别,我都针对同一产品有多个行。 (ex if a product have 3 categories, then it shows the product 3 times.) I just want to have 1 row for every product and the categories together separated by comma Can you help me please (例如,如果一个产品有3个类别,那么它将显示3次该产品。)我只想为每个产品设置1行,并且类别之间用逗号分隔,请您能帮我吗?

Try like this 这样尝试

$sql= "SET group_concat_max_len = 2048";
$sql = "select p.id,p.name,p.logo,p.short_description,  GROUP_CONCAT(c.category_name SEPARATOR ', ') as category_names   from eshop p join  eshop_cat c on
p.id=c.id GROUP BY p.id";

Just place your relational column p.id=c.id then it will work for you. 只需将您的p.id=c.id ,它将对您p.id=c.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM