警告:子查询返回的行数超过1

时间:2019-04-30 10:24:08

标签: php mysql

运行此查询时

SELECT a.sname,a.date,a.Roll_Number,b.branch,
                            COUNT(DISTINCT a.date) AS totaldays,

                            (SELECT COUNT(a.attendance_status)
                            FROM attendance as a , branch as b  
                            WHERE a.attendance_status='Present' and b.id='".$_GET['id']."' and  a.branch=b.branch  GROUP BY a.sid )
                             as present_days
                             FROM attendance as a , branch as b  
                             WHERE b.id='".$_GET['id']."'
                            and  a.branch=b.branch
                            GROUP BY a.sname ";

它显示此错误

  

警告:mysqli :: query():(21000/1242):子查询返回的行多于1

帮助我更正此查询。预先谢谢

1 个答案:

答案 0 :(得分:0)

对您的查询看来,您不需要present_days的子查询,并且避免重复此子查询,可以避免出现错误..

SELECT a.sname
  ,a.date,a.Roll_Number
  ,b.branch,
  COUNT(DISTINCT a.date) AS totaldays,
  COUNT(a.attendance_status) as present_days
  FROM attendance as a 
  INNER JOIN branch as b   ON a.attendance_status='Present' 
      and b.id='".$_GET['id']."' 
          and  a.branch=b.branch  
  GROUP BY a.sid 

无论如何,您都可能遭受sqlinjection的风险..您不应在sql中使用php var,而应使用准备好的语句和绑定参数..