2016-11-25 51 views
0

我正在使用MYSQL 5.7。我想要獲得最近的行不同的device_id。我想這些查詢:錯誤1055(42000):SELECT列表的表達式#1不在GROUP BY子句中...這與sql_mode = only_full_group_by不兼容

查詢1

SELECT `table`.`id`, `table`.`device_id` FROM `table` WHERE (id IN (SELECT id FROM (SELECT * FROM table ORDER BY date_modified DESC) AS last_modified GROUP BY device_id) and device_id <> ''); 

+----+------------------+ 
| id | device_id  | 
+----+------------------+ 
| 5 | ffcecafe5eed4fba | 
| 6 | ffcecafe5eed4fba | 
| 8 | 71085f00e527bae0 | 
+----+------------------+ 
3 rows in set (0.00 sec) 

但它不刪除重複項。

子查詢1

SELECT id FROM (SELECT * FROM table ORDER BY date_modified DESC) AS last_modified GROUP BY device_id; 
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'last_modified.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 

這給了錯誤。然後我在MySQL網站上找到使用ANY_VALUE()來刪除這個錯誤。

子查詢2

SELECT ANY_VALUE(id) FROM (SELECT * FROM table ORDER BY date_modified DESC) AS last_modified GROUP BY device_id; 
+---------------+------------------+ 
| ANY_VALUE(id) | device_id  | 
+---------------+------------------+ 
|    7 |     | 
|    8 | 71085f00e527bae0 | 
|    5 | ffcecafe5eed4fba | 
+---------------+------------------+ 
3 rows in set (0.00 sec) 

這是給不同的ID。但是當我在上面的查詢1中使用ANY_VALUE時,它給出了相同的結果。

如何在MySQL 5.7中查詢不同的最近行?


可能重複

MySQL 5.7 return all columns of table based on distinct column

+1

請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple- sql-query – Strawberry

+0

id是你的附屬軟件嗎? –

+0

@Strawberry我不是要求爲我做一個查詢,我得到這個錯誤ERROR 1055(42000):SELECT列表的表達式#1不在GROUP BY子句中,並且包含非聚集列'last_modified.id',這在功能上不是依賴於GROUP BY子句中的列;這是不符合sql_mode = only_full_group_by –

回答

1

這是工作了我 -

SELECT m1.* 
FROM messages m1 LEFT JOIN messages m2 
ON (m1.name = m2.name AND m1.id < m2.id) 
WHERE m2.id IS NULL; 

感謝@Shadow的鏈接 possible duplicate查詢。

相關問題