Om du använder MySQL 8+ kan vi hantera detta med ROW_NUMBER
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Date ORDER BY Time) rn
FROM attendancedata
WHERE EnrolledID = 23
)
SELECT
Date,
MAX(CASE WHEN rn = 1 THEN Time END) AS PO1,
MAX(CASE WHEN rn = 2 THEN Time END) AS PO2,
MAX(CASE WHEN rn = 3 THEN Time END) AS PO3,
MAX(CASE WHEN rn = 4 THEN Time END) AS PO4,
MAX(CASE WHEN rn = 5 THEN Time END) AS PO5,
MAX(CASE WHEN rn = 6 THEN Time END) AS PO6,
MAX(CASE WHEN rn = 7 THEN Time END) AS PO7
FROM cte
GROUP BY
Date
ORDER BY
Date;
Men observera att det i allmänhet är önskvärt att lagra datum och tid i en enda datetime
fält med MySQL. I just det här fallet fungerade det bra, men det kanske inte alltid är sant.