从mysql中的多列中选择一列的不同

时间:2016-12-01 11:03:00

标签: mysql sql

我有这个问题。

select Leaf_id,product_id_x,product_id_y,count from table;

我正在寻找的是选择不同的Leaf_id的所有值。

所以我试过

select Leaf_id,product_id_x,product_id_y,count from table group by Leaf_id;

但收到此错误。

SELECT list is not in GROUP BY clause and contains nonaggregated column
 'table.product_id_x' which is not functionally dependent on columns in 
GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")

我应该对所有列进行分组 ?

3 个答案:

答案 0 :(得分:1)

为什么不能使用distinct关键字

select distinct Leaf_id,product_id_x,product_id_y,`count` from `table`;

答案 1 :(得分:0)

如果要为每列选择所有值,请使用聚合函数。也许你想要group_concat()

select Leaf_id, group_concat(distinct product_id_x),
       group_concat(distinct product_id_y),
       group_concat(distinct count)
from table
group by leaf_id;

答案 2 :(得分:0)

select
 max(Leaf_id) as Leaf_ID
,product_id_x
,product_id_y
,count

from table

group by product_id_x, product_id_y, count

这可能有帮助吗?显然

Min(Leaf_ID) as Leaf_ID

会很好用。

希望这有帮助