MySql 5.7错误(与sql_mode = only_full_group_by不兼容)

时间:2019-01-21 16:41:31

标签: python mysql group-by

我看到了几个回答可以解决您的查询的答案,但是我的查询中有GROUP BY CONCAT,而且不确定如何解决。

 cur.execute('''
    SELECT user_id, symbol FROM first_sweep    
    WHERE date_time BETWEEN %s AND %s AND action = 'buy'
    GROUP BY CONCAT(user_id, "-",symbol) 
    ''', (min_rng, max_rng,))    
    fs = cur.fetchall() 

我得到的全部错误是:

(1055, "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'stockpicker.first_sweep.user_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")

我正在使用Ubuntu 18.04,MySql 5.7和Python 2.7

1 个答案:

答案 0 :(得分:1)

这有效:

SELECT 
  CONCAT(user_id, '-', symbol) as usym, 
  MAX(user_id) AS user_id, 
  MAX(symbol) AS symbol
FROM first_sweep    
WHERE date_time BETWEEN %s AND %s AND action = 'buy'
GROUP BY usym