mysql不兼容的sql模式

时间:2018-02-17 17:23:03

标签: mysql sql

我改变了我的服务器,并且有最新的mysql。

首先我的询问:

SELECT userdata.ONLINE,pictures.IMAGELINK,userdata.BIRTHDATE,userdata.POSITION,InfiniteLove.USERNAME  FROM InfiniteLove
                                  LEFT JOIN pictures ON InfiniteLove.USERNAME = pictures.USERNAME
                                  LEFT JOIN userdata ON InfiniteLove.USERNAME = userdata.USERNAME
                                  WHERE NOT InfiniteLove.USERNAME = 'lalox3' AND InfiniteLove.GENDER = 'M' AND InfiniteLove.SEARCH = 'M' OR InfiniteLove.SEARCH = 'B'
                                  AND NOT InfiniteLove.GENDER = 'W' GROUP BY InfiniteLove.USERNAME

错误:

#1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'simplylabs.pictures.IMAGELINK' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

我真的不得不说,如果我不得不与一张桌子一起工作,我真是太蠢了。

我无法更改mysql设置,因此我的查询必须更改。

2 个答案:

答案 0 :(得分:1)

您有GROUP BY子句但没有聚合函数。这导致可能根本不需要它:

SELECT u.ONLINE, p.IMAGELINK, u.BIRTHDATE, u.POSITION, il.USERNAME
FROM InfiniteLove il LEFT JOIN
     pictures p
     ON il.USERNAME = p.USERNAME LEFT JOIN
     userdata u
     ON il.USERNAME = u.USERNAME
WHERE il.USERNAME <> 'lalox3' AND il.GENDER = 'M' AND 
      il.SEARCH IN ('M', 'B') AND 
      il.GENDER <> 'W';

请注意使用<>而不是not . . . =。这是更传统的语法。我还将OR合并为IN。这与你的逻辑不一样,但我怀疑它是你真正想要的。

如果您想要每个用户一行,那么您需要一个聚合功能。这可能是你想要的:

SELECT u.ONLINE, GROUP_CONCAT(p.IMAGELINK) as imagelinks,
       u.BIRTHDATE, u.POSITION, il.USERNAME
FROM InfiniteLove il LEFT JOIN
     pictures p
     ON il.USERNAME = p.USERNAME LEFT JOIN
     userdata u
     ON il.USERNAME = u.USERNAME
WHERE il.USERNAME <> 'lalox3' AND il.GENDER = 'M' AND 
      il.SEARCH IN ('M', 'B') AND 
      il.GENDER <> 'W'
GROUP BY u.ONLINE, u.BIRTHDATE, u.POSITION, il.USERNAME;

这将所有图像合并为一列。

答案 1 :(得分:1)

您正在使用group by without aggregation功能 这在sql中已弃用,在大多数数据库中都不允许 如果你想避免重复的结果,请不要使用group by distintc

  SELECT DISTINCT  userdata.ONLINE
        ,pictures.IMAGELINK
        ,userdata.BIRTHDATE
        ,userdata.POSITION
        ,InfiniteLove.USERNAME  
  FROM InfiniteLove
  LEFT JOIN pictures ON InfiniteLove.USERNAME = pictures.USERNAME
  LEFT JOIN userdata ON InfiniteLove.USERNAME = userdata.USERNAME
  WHERE NOT InfiniteLove.USERNAME = 'lalox3' 
  AND InfiniteLove.GENDER = 'M' 
  AND (InfiniteLove.SEARCH = 'M' OR InfiniteLove.SEARCH = 'B')
  AND NOT InfiniteLove.GENDER = 'W'