Tidak ada perintah PIVOT di MySQL, tetapi ada solusi - Dasar-dasar tabel pivot:baris ke kolom... , Tabel pivot MySQL (mengubah baris menjadi kolom) .
Untuk kasus Anda, Anda dapat menggunakan sesuatu seperti ini -
SELECT AppNo,
MAX(IF(code = 1, Location, NULL)) Location,
MAX(IF(code = 1, Department, NULL)) Department,
1 Code1,
MAX(IF(code = 2, Location, NULL)) Location,
MAX(IF(code = 2, Department, NULL)) Department,
2 Code2,
MAX(IF(code = 3, Location, NULL)) Location,
MAX(IF(code = 3, Department, NULL)) Department,
3 Code3,
MAX(IF(code = 4, Location, NULL)) Location,
MAX(IF(code = 4, Department, NULL)) Department,
4 Code4,
MAX(IF(code = 5, Location, NULL)) Location,
MAX(IF(code = 5, Department, NULL)) Department,
5 Code5
FROM <table name>
GROUP BY AppNo
P.S. Anda dapat menjalankan kueri ini dari rutinitas yang tersimpan.