错误代码: 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

你可能感兴趣的:(MySQL)