2016-12-01 87 views
-2

我有这个查询。从mysql中的多个列中选择不同的列

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") 

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

+0

如果您注意到,启用了'sql_mode = only_full_group_by'。要根据一列进行区分,请使用SET sql_mode =''' – Viki888

+0

添加示例表格数据和预期结果 - 以及格式化文本。 – jarlh

+0

强烈建议'only_full_group_by'。 – jarlh

回答

1

为什么你不能使用distinct关键字

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

它会从所有列中选择不同的值吗? – Shubham

+0

所有选定的列。 – jarlh

+0

@SRingne,组合中所有选定列的不同 – Rahul

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; 
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 

会工作得很好。

希望这会有所帮助

+0

GROUP BY无效。 (一般的GROUP BY规则说:如果指定了一个GROUP BY子句,SELECT列表中的每个列引用必须标识一个分组列或者是一个设置函数的参数!) – jarlh

+0

我打算按其他任何方法进行分组......哎呀! –

相关问题