Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CharlesK's avatar

Mysql query returns error

I am learning Charts in Laravel, i need to draw Line graph for daily Student Attendance for those students come late or on time. I tried to write MYSQL query but it doesn't work

I tried subquery on same table to get data for daily students and i also need 7 dates only not full date, like date is stored in db as 09/08/2019 but i need it as 08 as date.

SELECT Date, COUNT(*) AS TimeStudent
   FROM attendance WHERE `Attendance`='OnTime' AND (SELECT COUNT(*) AS 
   LateStudent FROM attendance 
   WHERE `Attendance`='Late'
   GROUP BY `Date`
   ORDER BY LateStudent DESC) 
   GROUP BY `Date`
   ORDER BY TimeStudent DESC 

but i got

[Err] 1241 - Operand should contain 1 column(s)

, because i can't use to fetch Date again in subquery while use it after where clause. Any one help me plz.

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@charlesk

Try this-

SELECT 
    `Date`,
    DATE_FORMAT(`Date`, '%d') AS Month_Date,  -- You can modify it as per your requirement
    SUM(IF(`Attendance` = 'OnTime', 1, 0)) AS OnTime_Count,
    SUM(IF(`Attendance` = 'Late', 1, 0)) AS Late_Count
FROM attendance
WHERE `Date` >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY `Date`;
2 likes

Please or to participate in this conversation.