我的SQL语句在另一台本地服务器上工作正常,但在最近安装的灯服务器中尝试我的项目后,它显示以下错误 -
SELECT列表的表达式#2不在GROUP BY子句中并且包含 nonaggregated column' newmesssages.send_date'不是 功能上依赖于GROUP BY子句中的列;这是 与sql_mode = only_full_group_by
不兼容
声明 -
SELECT COUNT(reciever) as total, send_date, message, sender_username, sender_name
FROM (SELECT * FROM messages_view WHERE reciever = 17 AND viewed_by_reciever is NULL order by send_date DESC)
AS newmesssages
GROUP BY sender ORDER BY send_date DESC LIMIT 20
答案 0 :(得分:1)
您需要按所有未汇总的列进行分组,如下所示:
SELECT COUNT(reciever) as total, send_date, message, sender_username, sender_name
FROM (SELECT *
FROM messages_view
WHERE reciever = 17 AND viewed_by_reciever is NULL order by send_date DESC)
AS newmesssages
GROUP BY send_date, message, sender_username, sender_name
ORDER BY send_date DESC LIMIT 20
如果您不想按这些列进行分组,则需要将它们从SELECT中删除。
另一个解决方案是改变sql_mode这样做:
SET SESSION sql_mode = REPLACE(@@sql_mode, ',only_full_group_by', '');
最后,您的SQL中出现了错误,您按sender
进行分组但没有该列名称
<强> EDITED 强>: 你可以这样做:
SELECT (SELECT COUNT(reciever)
FROM (SELECT *
FROM messages_view
WHERE reciever = 17 AND viewed_by_reciever is NULL
order by send_date DESC) AS newmesssages
GROUP BY send_date, message, sender_username, sender_name),
send_date, message, sender_username, sender_name
FROM (SELECT *
FROM messages_view
WHERE reciever = 17 AND viewed_by_reciever is NULL order by send_date DESC)
ORDER BY send_date DESC LIMIT 20
我认为这可以起作用
答案 1 :(得分:0)
此查询代码ONLY_FULL_GROUP_BY
禁用
对于Mysql:-在查询中使用此代码:
$set_mode = "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))"; $resultss = mysqli_query($this->dbh,$set_mode);
对于codeigniter:-在查询中使用此代码:
$this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));") ;
关于你:
$set_mode = "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))"; $resultss = mysqli_query($this->dbh,$set_mode);
SELECT COUNT(reciever) as total, send_date, message, sender_username, sender_name
FROM (SELECT * FROM messages_view WHERE reciever = 17 AND viewed_by_reciever is NULL order by send_date DESC)
AS newmesssages
GROUP BY sender ORDER BY send_date DESC LIMIT 20