2016-12-01 110 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

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

相關問題