Table:
laterecords
-----------
studentid - varchar
latetime - datetime
reason - varchar
My Query:
SELECT laterecords.studentid,
laterecords.latetime,
laterecords.reason,
( SELECT Count(laterecords.studentid) FROM laterecords
GROUP BY laterecords.studentid ) AS late_count
FROM laterecords
I’m getting ” MySQL Subquery Returns more than one row” error.
I know a workaround for this query to use the following query:
SELECT laterecords.studentid,
laterecords.latetime,
laterecords.reason
FROM laterecords
Then using php loop to though the results and do below query to get the late_count and echo it out:
SELECT Count(laterecords.studentid) AS late_count FROM laterecords
But i think there might be a better solution ?
The simple fix is to add a
WHEREclause in your subquery:A better option (in terms of performance) is to use a join: