Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL fragt zwei Tabellen ab und erhält Daten datums- oder benutzerweise

Das würde reichen:

SELECT 
    u.lastname AS `User`
  , SUM(CASE WHEN activitydate='2011-04-01' THEN time ELSE 0 END) AS `2011-04-01`
  , SUM(CASE WHEN activitydate='2011-04-02' THEN time ELSE 0 END) AS `2011-04-02`
  , ...
  , SUM(CASE WHEN activitydate='2011-04-30' THEN time ELSE 0 END) AS `2011-04-30`
FROM Hours h
  JOIN Users u
    ON h.userid = u.userid
WHERE h.activitydate BETWEEN '2011-04-01' AND '2011-04-30'
GROUP BY h.userid
ORDER BY h.userid