久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

解決mysql錯(cuò)誤:Subquery returns more than 1 row問題

瀏覽:75日期:2023-05-05 10:11:53
目錄
  • mysql錯(cuò)誤:Subquery returns more than 1 row
    • 報(bào)錯(cuò)如下
    • 解決方法
  • 錯(cuò)誤代碼:1242 Subquery returns more than 1 row
    • 錯(cuò)誤描述
    • 錯(cuò)誤原因
    • 解決辦法
  • 總結(jié)

    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ò)如下

    解決方法

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

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

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

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

    2)在子查詢條件語句加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語句時(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 t0on 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 t0on t0.id = t.stuNo

    總結(jié)

    以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持。

    標(biāo)簽: MySQL
    主站蜘蛛池模板: 丹巴县| 吉安市| 舞钢市| 玉山县| 霍山县| 台中市| 淄博市| 利辛县| 德化县| 金坛市| 舒兰市| 外汇| 和硕县| 北京市| 澄江县| 平江县| 六安市| 县级市| 南川市| 平邑县| 板桥市| 新丰县| 手机| 江山市| 舞阳县| 台江县| 甘孜| 武穴市| 三原县| 女性| 页游| 曲周县| 客服| 瑞丽市| 隆子县| 海原县| 灵武市| 台江县| 武陟县| 新巴尔虎左旗| 北安市|