国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

MySQL報(bào)錯(cuò) Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column解決方法

這篇具有很好參考價(jià)值的文章主要介紹了MySQL報(bào)錯(cuò) Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column解決方法。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

報(bào)錯(cuò)原因

使用GROUP BY 語(yǔ)句違背了sql_mode=only_full_group_by,在MySQL數(shù)據(jù)庫(kù)版本為5.7以上的版本,默認(rèn)開啟了 ONLY_FULL_GROUP_BY SQL模式,在此模式下,對(duì)于group by操作,如果在select語(yǔ)句中的查詢列沒有在group by中出現(xiàn),那么這個(gè)SQL就是非法的,因?yàn)榱胁辉趃roup by語(yǔ)句中,所以設(shè)置了sql_mode=only_full_group_by的數(shù)據(jù)庫(kù),在使用group by時(shí)就會(huì)報(bào)錯(cuò),換句話說,拒絕選擇列表、HAVING 條件或 ORDER BY 列表引用非聚合列的查詢,這些列既不在 GROUP BY 子句中命名,也不在功能上依賴于(唯一確定的)GROUP BY 列。

注意

不是說SELECT xx,xx必須是GROUP BY中的列,如SELECT 聚集函數(shù)(不在GROUP中的列)也能正常執(zhí)行

例如

現(xiàn)有兩表Student表以及SC表(選課表)。

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

執(zhí)行語(yǔ)句

select cno,count(sc.sno),count(student.sno) from student,sc group by cno;

紅色部分可以發(fā)現(xiàn)sc.sno與studen.sno都沒有在GROUP BY的列中,但執(zhí)行語(yǔ)句依舊正常,因?yàn)槭褂昧司奂瘮?shù)。

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

而不把sc.sno放在聚集函數(shù)中,則執(zhí)行錯(cuò)誤。

select cno,sc.sno from student,sc group by cno;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

解決方法

臨時(shí)解決(重啟mysqld后失效)

1.直接在mysql-cli層面做設(shè)置

select @@global.sql_mode;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

2.將ONLY_FULL_GROUP_BY從sql_mode中移除:

set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

3.重啟MySQL客戶端

注意不要關(guān)閉mysqld,否則修改會(huì)復(fù)原。

在MySQL-server層面修改sql_mode

永久生效,重啟依然有效

優(yōu)化mysql語(yǔ)句

使用派生表查詢

例子

BOOKSTORAGE表:

存書(書號(hào),書名,出版社,版次,出版日期,作者,書價(jià),進(jìn)價(jià),數(shù)量)

CREATE TABLE BOOKSTORAGE(
isbn VARCHAR(10) PRIMARY KEY,
name VARCHAR(10) NOT NULL,
publisher VARCHAR(10), 
edition VARCHAR(10),
date DATE,
writer VARCHAR(10),
price FLOAT CHECK(price>=0),
pprice FLOAT CHECK(pprice>=0),
number INT CHECK(number>=0)
);

銷售(日期,書號(hào),數(shù)量,單價(jià))

CREATE TABLE SALE(
date DATE,
isbn VARCHAR(10),
number INT CHECK(number>=0),
price FLOAT CHECK(price>=0),
FOREIGN KEY(isbn) REFERENCES BOOKSTORAGE(isbn)
);

注意先建立BOOKSTORAGE表再建立SALE表,因?yàn)镾ALE表外鍵參考BOOKSTORAGE,所以要先建立BOOKSTORAGE

插入數(shù)據(jù)

INSERT
INTO BOOKSTORAGE
VALUES('1','西游記','人民文學(xué)出版社','第三版','2003/1/1','吳承恩',20.1,15.5,150),
('2','水滸傳','人民文學(xué)出版社','第二版','2005/9/1','施耐庵',23.9,17.1,200),
('3','三國(guó)演義','中華書局出版社','第五版','2008/6/1','羅貫中',26.3,15.8,210),
('4','紅樓夢(mèng)','人民文學(xué)出版社','第四版','2001/6/1','曹雪芹',22.3,17.2,190);
INSERT
INTO SALE
VALUES('2023-3-12','1','30',17.8),
('2023-3-15','1','20',20.1),
('2023-3-12','2','25',18.8),
('2023-3-15','2','25',23.9),
('2023-3-12','3','15',18.6),
('2023-3-15','3','30',26.3),
('2023-3-12','4','22',19.5),
('2023-3-15','4','12',22.3);

注意先插入BOOKSTORAGE,因?yàn)镾ALE參考BOOKSTORAGE

插入結(jié)果

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

求解問題

列出所有日期的銷售報(bào)表,包括書名、數(shù)量和合計(jì)金額(每一種書的銷售總金額)

方法1修改sql_mode

mysql語(yǔ)句

SELECT SALE.date,BOOKSTORAGE.name,SALE.number,ROUND(SUM(SALE.price*SALE.number),2) money
FROM SALE,BOOKSTORAGE 
WHERE SALE.isbn=BOOKSTORAGE.isbn
GROUP BY SALE.isbn,SALE.DATE 
ORDER BY SALE.date;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

查詢出所有的sql_mode

select @@global.sql_mode;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

移除ONLY_FULL_GROUP_BY

set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

重啟mysql后,執(zhí)行語(yǔ)句

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

查詢出所有的sql_mode

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

發(fā)現(xiàn)沒有ONLY_FULL_GROUP_BY

重啟mysqld后,再次查詢sql_mode

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

發(fā)現(xiàn)有ONLY_FULL_GROUP_BY

方法2優(yōu)化mysql語(yǔ)句

SELECT SALE2.DATE,name,SALE2.number,SALE2.PRICE
FROM BOOKSTORAGE,(SELECT isbn,date,SUM(number),ROUND(SUM(price*number),2) FROM SALE GROUP BY SALE.isbn,SALE.DATE) AS SALE2(isbn,date,number,price)
WHERE SALE2.isbn=BOOKSTORAGE.isbn
ORDER BY SALE2.DATE ASC;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

思考

問題

student表結(jié)構(gòu)

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

執(zhí)行mysql語(yǔ)句,select 選擇的列不僅僅只有sno,為什么正常輸出?

select * from student group by sno;

of select list is not in group by clause and contains nonaggregated column,個(gè)人經(jīng)驗(yàn),mysql,數(shù)據(jù)庫(kù)

答案

因?yàn)閟no屬性是主碼,即primary key,經(jīng)測(cè)試發(fā)現(xiàn)group by **,**為主碼的屬性名時(shí),select語(yǔ)句中的查詢列沒有在group by中出現(xiàn),也是允許的。文章來源地址http://www.zghlxwxcb.cn/news/detail-764659.html

到了這里,關(guān)于MySQL報(bào)錯(cuò) Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 解決 Uncaught (in promise) TypeError: list is not iterable 報(bào)錯(cuò)

    解決 Uncaught (in promise) TypeError: list is not iterable 報(bào)錯(cuò)

    最近在項(xiàng)目中遇到 Uncaught (in promise) TypeError: list is not iterable 報(bào)錯(cuò),雖然不影響代碼運(yùn)行,但是看著報(bào)錯(cuò)感覺有點(diǎn)難受,試試能不能解決它 看了很多篇文章,都是說使用 Object.keys() 可以解決問題 就先使用 Object.keys() 看看,代碼運(yùn)行之后 因?yàn)?Object.keys() 傳入的是 null 和 undefin

    2024年02月11日
    瀏覽(21)
  • MySQL 出現(xiàn) which is not functionally dependent on columns in GROUP BY clause;解決方法

    項(xiàng)目跑到一個(gè)新服務(wù)器上保存了 一個(gè)新安裝的數(shù)據(jù)庫(kù),出現(xiàn)了問題 具體報(bào)錯(cuò)信息如下: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘a(chǎn)igcc.t2.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 問題出現(xiàn)在grou

    2024年02月08日
    瀏覽(19)
  • make 報(bào)錯(cuò):’XX‘ is not a member of ‘std‘或者 ’XX‘ in namespace ‘std‘ does not name a template type 報(bào)錯(cuò)解決方法

    Gazebo仿真時(shí)遇到的問題, 可能情況之一是std標(biāo)準(zhǔn)庫(kù)版本太低, 可以通過在CmakeLists.txt中添加語(yǔ)句解決: set(CMAKE_CXX_FLAGS \\\"${CMAKE_CXX_FLAGS} -std=c++17\\\") (根據(jù)自己實(shí)際情況修改,現(xiàn)在std的版本貌似已經(jīng)到23了)

    2024年01月16日
    瀏覽(20)
  • linux報(bào)錯(cuò) E: Type ‘‘deb‘ is not known on line 1 in source list /etc/apt/sources.list.d/docker.【最新解決辦法】

    linux報(bào)錯(cuò) E: Type ‘‘deb‘ is not known on line 1 in source list /etc/apt/sources.list.d/docker.【最新解決辦法】

    你是否出現(xiàn)這種情況 ?sudo apt-get update? 結(jié)果就 the \\\'deb\\\'報(bào)錯(cuò),在網(wǎng)上查了許多文獻(xiàn)都沒啥作用,而且許多都過時(shí)了,終于讓我在茫茫人海之中找到一位大佬的評(píng)論,淚水在我眼眶里打架,搞了幾個(gè)小時(shí)終于有結(jié)果了。 出現(xiàn)這種報(bào)錯(cuò)之后,你會(huì)發(fā)現(xiàn)sudo 之類的命令大多都用不起

    2024年04月10日
    瀏覽(87)
  • 加入k8s集群報(bào)錯(cuò)this Docker version is not on the list of validated versions: 20.10.17. Latest validated...

    加入k8s集群報(bào)錯(cuò)this Docker version is not on the list of validated versions: 20.10.17. Latest validated...

    報(bào)錯(cuò)信息可以看出跟docker的版本有關(guān)系,意思是:此 Docker 版本不在已驗(yàn)證版本列表中:20.10.17。 最新驗(yàn)證版本:18.09 分別查看docker和k8s的版本 k8s與docker的兼容關(guān)系圖,所以需要降低docker的版本到18.09以下 查看當(dāng)前倉(cāng)庫(kù)支持的docker版本 選擇18.09的版本,降級(jí)到18.06版本會(huì)報(bào)錯(cuò)

    2024年02月14日
    瀏覽(22)
  • adb no permissions (user *** is not in the plugdev group)

    首次配置ubuntu下的adb 環(huán)境,執(zhí)行了adb device命令會(huì)出現(xiàn)以下問題 嘗試了下 sudo ./adb devices 還是一樣的錯(cuò)誤 可以嘗試修改可執(zhí)行文件? adb 的權(quán)限 這樣就可以找到設(shè)備了。

    2024年02月15日
    瀏覽(24)
  • which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod

    which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod

    mysql 執(zhí)行報(bào)錯(cuò) : Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘bcdsystem.cities.city’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\\\" 1.這個(gè)錯(cuò)誤發(fā)生在mysql 5.7 版本及以上版本會(huì)出現(xiàn)的問題: 2.在sql執(zhí)行時(shí),出現(xiàn)

    2024年02月16日
    瀏覽(24)
  • kafka消費(fèi)者報(bào)錯(cuò)Offset commit ......it is likely that the consumer was kicked out of the group的解決

    2022年10月份接到一個(gè)小功能,對(duì)接kafka將數(shù)據(jù)寫到數(shù)據(jù)庫(kù),開始的需求就是無(wú)腦批量insert,隨著時(shí)間的推移,業(yè)務(wù)需求有變更,kafka的生產(chǎn)消息頻次越來越高,到今年7月份為止就每秒會(huì)有幾十條甚至上百條,然后消費(fèi)消息的代碼就報(bào)錯(cuò): Caused by: org.apache.kafka.clients.consumer.Com

    2024年02月07日
    瀏覽(23)
  • 小程序正式版報(bào)錯(cuò)600002 url not in domain list

    小程序正式版報(bào)錯(cuò)600002 url not in domain list

    開發(fā)版,體驗(yàn)版都沒問題,線上版本報(bào)錯(cuò) 在小程序官方文檔找到錯(cuò)誤碼 600002 : 登錄小程序后臺(tái),找到開發(fā) - 開發(fā)管理 - 開發(fā)設(shè)置 - 服務(wù)器域名 配置對(duì)應(yīng)的域名即可。

    2024年02月16日
    瀏覽(27)
  • kafka-報(bào)錯(cuò)-The coordinator is not aware of this member

    我在項(xiàng)目里把原來用著的 獨(dú)立消費(fèi)者 consumer-group-id 同時(shí)當(dāng)做消費(fèi)者組來消費(fèi)分區(qū)信息,導(dǎo)致協(xié)調(diào)器找不到這個(gè) consumer-group-id 注冊(cè)兩個(gè)測(cè)試 topic 寫一個(gè)消費(fèi)者組 啟動(dòng)項(xiàng)目,發(fā)送消息 報(bào)錯(cuò)日志 查看kafka服務(wù)器的消費(fèi)者狀態(tài)

    2024年02月13日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包