国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

解決mysql錯誤:Subquery returns more than 1 row問題

瀏覽:138日期:2023-05-05 10:11:53
目錄
  • mysql錯誤:Subquery returns more than 1 row
    • 報錯如下
    • 解決方法
  • 錯誤代碼:1242 Subquery returns more than 1 row
    • 錯誤描述
    • 錯誤原因
    • 解決辦法
  • 總結

    mysql錯誤:Subquery returns more than 1 row

    mysql報錯:SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

    錯誤的意思是指子查詢結果多于一行。

    報錯如下

    解決方法

    以這個sql語句為例

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

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

    (我實際開發中遇到的就是數據重復寫入的情況,在數據庫查到有相同的數據兩條,這不符原本的原本的業務需求)

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

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

    3)在子查詢前加any關鍵字

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

    錯誤代碼:1242 Subquery returns more than 1 row

    錯誤描述

    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...

    錯誤代碼: 1242Subquery returns more than 1 row

    執行耗時 : 0.009 sec傳送時間 : 0.002 sec總耗時 : 0.012 sec

    錯誤原因

    在編寫查詢SQL語句時,其中有個字段是從另一張表里獲取

    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是多條數據,而外層查詢結果是要求num為一條數據

    解決辦法

    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

    總結

    以上為個人經驗,希望能給大家一個參考,也希望大家多多支持。

    標簽: MySQL
    主站蜘蛛池模板: 淮阳县| 河西区| 福泉市| 阿拉尔市| 乐亭县| 邵阳县| 石家庄市| 蚌埠市| 馆陶县| 邯郸县| 镇巴县| 潜江市| 伊宁县| 双江| 张北县| 庄河市| 门源| 井研县| 伊春市| 稷山县| 百色市| 平湖市| 阿鲁科尔沁旗| 炎陵县| 台安县| 株洲县| 什邡市| 永昌县| 佳木斯市| 通许县| 阿拉善左旗| 辉县市| 河北区| 来宾市| 翼城县| 遂平县| 根河市| 江西省| 安西县| 定西市| 视频|