1. 程式人生 > >錯誤程式碼: 1242 Subquery returns more than 1 row

錯誤程式碼: 1242 Subquery returns more than 1 row

1. 錯誤描述

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

錯誤程式碼: 1242
Subquery returns more than 1 row

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

2. 錯誤原因

在編寫查詢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 t0
on t0.id = t.stuNo

查詢出num是多條資料,而外層查詢結果是要求num為一條資料

3. 解決辦法

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