Đây là hai phương pháp khác, chỉ để hoàn thiện:
-
SELECT p.name, MAX(CASE t.phonetype WHEN 'work' THEN t.phoneno END) AS workphone, MAX(CASE t.phonetype WHEN 'home' THEN t.phoneno END) AS homephone, MAX(CASE t.phonetype WHEN 'mob' THEN t.phoneno END) AS mobilephone FROM people p INNER JOIN phones t ON p.Id = t.Id GROUP BY p.Id, p.Name
-
SELECT p.name, w.phoneno AS workphone, h.phoneno AS homephone, m.phoneno AS mobilephone FROM people p LEFT JOIN phones w ON p.Id = w.Id AND w.phonetype = 'work' LEFT JOIN phones h ON p.Id = h.Id AND h.phonetype = 'home' LEFT JOIN phones m ON p.Id = m.Id AND m.phonetype = 'mob'