Mysql
 sql >> Database >  >> RDS >> Mysql

Mysql interroga due tabelle e ottiene dati in termini di data o utente

Questo farebbe:

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