查询:
SELECT `category`.*,
Group_concat(DISTINCT `sub_category`.`english_name` ORDER BY
`sub_category`.`order_number`
ASC) AS `sub_category`,
Group_concat(DISTINCT `sub_category`.`id` ORDER BY
`sub_category`.`order_number`
ASC) AS `sub_category_id`,
Group_concat(`sub_category`.`status` ORDER BY
`sub_category`.`order_number` ASC)
AS `sub_category_status`
FROM `category`
LEFT JOIN `item_category`
ON `item_category`.`category` = `category`.`id`
LEFT JOIN `sub_category`
ON `sub_category`.`category` = `category`.`id`
GROUP BY `category`.`id`
ORDER BY `category`.`order_number` ASC
有问题的一行:
Group_concat(
sub_category
。status
ORDER BYsub_category
。order_number
ASC)
结果:
(
[ID] => 22
[Create_date] => 2017-11-20
[Created_by] => 0
[English_name] => Pens & refills
[Gujarati_name] => પેન અનેર રીફીલ
[Header] => False
[Sidebar] => False
[Order_number] => 2
[Status] => Close
[sub_category] => Botteled Ink,Fountain Pens,gel ink rollball pens
[sub_category_id] => 54,55,56
[sub_category_status] => Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open,Open
)
正如您所看到的,我在可以使用的地方使用了DISTINCT。状态为打开或关闭,因此DISTINCT在此列中没有帮助。任何人都可以告诉我如何解决这个问题?
答案 0 :(得分:0)
您错过了DISTINCT
对于sub_category_status
这就是为什么您会获得重复值
Group_concat(`sub_category`.`status` ORDER BY
`sub_category`.`order_number` ASC)
AS `sub_category_status`
还为上一栏添加DISTINCT
Group_concat( DISTINCT `sub_category`.`status` ORDER BY
`sub_category`.`order_number` ASC)
AS `sub_category_status`