溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mysql錯(cuò)誤Subquery?returns?more?than?1?row怎么解決

發(fā)布時(shí)間:2023-05-05 11:22:48 來(lái)源:億速云 閱讀:290 作者:iii 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章主要介紹了mysql錯(cuò)誤Subquery returns more than 1 row怎么解決的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇mysql錯(cuò)誤Subquery returns more than 1 row怎么解決文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

    mysql錯(cuò)誤:Subquery returns more than 1 row

    mysql報(bào)錯(cuò):SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

    錯(cuò)誤的意思是指子查詢結(jié)果多于一行。

    報(bào)錯(cuò)如下

    mysql錯(cuò)誤Subquery?returns?more?than?1?row怎么解決

    解決方法

    以這個(gè)sql語(yǔ)句為例 

    select * from table1 where table1.colums=(select columns from table2);

    1)如果是寫入重復(fù),去掉重復(fù)數(shù)據(jù)。然后寫入的時(shí)候,可以加邏輯判斷(php)或者外鍵(mysql),防止數(shù)據(jù)重復(fù)寫入。

    (我實(shí)際開(kāi)發(fā)中遇到的就是數(shù)據(jù)重復(fù)寫入的情況,在數(shù)據(jù)庫(kù)查到有相同的數(shù)據(jù)兩條,這不符原本的原本的業(yè)務(wù)需求)

    2)在子查詢條件語(yǔ)句加limit 1,找到一個(gè)符合條件的就可以了

    select * from table1 where table1.colums=(select columns from table2 limit 1);

    3)在子查詢前加any關(guān)鍵字

    select * from table1 where table1.colums=any(select columns from table2);

    錯(cuò)誤代碼:1242 Subquery returns more than 1 row

    錯(cuò)誤描述

    1 queries executed, 0 success, 1 errors, 0 warnings

    查詢:SELECT t.id, DATE_FORMAT( t.statisTime, '%Y-%m-%d %H:%i:%s' ) statisTime, (SELECT `id` FROM t_truck_info WHERE id = t.plateId...

    錯(cuò)誤代碼: 1242Subquery returns more than 1 row

    執(zhí)行耗時(shí)   : 0.009 sec傳送時(shí)間   : 0.002 sec總耗時(shí)      : 0.012 sec

    錯(cuò)誤原因

    在編寫查詢SQL語(yǔ)句時(shí),其中有個(gè)字段是從另一張表里獲取

    select t.id,(select num from t_user_info where id = stuNo) as amount from t_stu_info t left join t_user_info t0
    on t0.id = t.stuNo

    查詢出num是多條數(shù)據(jù),而外層查詢結(jié)果是要求num為一條數(shù)據(jù)

    解決辦法

    select t.id,(select sum(num) from t_user_info where id = stuNo) as amount from t_stu_info t left join t_user_info t0
    on t0.id = t.stuNo

    關(guān)于“mysql錯(cuò)誤Subquery returns more than 1 row怎么解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“mysql錯(cuò)誤Subquery returns more than 1 row怎么解決”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI