Rumah > Soal Jawab > teks badan
Saya melihat soalan temuduga yang disiarkan oleh orang lain dalam kumpulan, =. =, bagaimana untuk bertanya?
黄舟2017-06-22 11:56:36
Gambar tajuk ialah Chinase, klik di sini.
Kes yang ditentukan kursus:
Pelaksanaan KES:
SELECT
`Name`,
MAX(
CASE
WHEN Course = 'Chinase' THEN
Score
END
) AS Chinase,
MAX(
CASE
WHEN Course = 'Math' THEN
Score
END
) AS Math,
MAX(
CASE
WHEN Course = 'English' THEN
Score
END
) AS English
FROM
table1
GROUP BY
`Name`
JIKA pelaksanaan:
SELECT
`Name`,
SUM(IF (Course = 'Chinase', Score, 0)) AS Chinase,
SUM(IF (Course = 'Math', Score, 0)) AS Math,
SUM(IF (Course = 'English', Score, 0)) AS English
FROM
table1
GROUP BY `Name`
Kursus tidak pasti, gunakan dinamik:
SET @CC='';
SELECT @CC:=CONCAT(@CC,'SUM(IF(Course=\'',Course,'\'',',Score,0)) AS ',Course,',') FROM (SELECT DISTINCT Course FROM table1) A;
SET @HH=CONCAT('SELECT Name,',LEFT(@CC,LENGTH(@CC)-1),' FROM table1 GROUP BY Name');
PREPARE stmt FROM @HH;
EXECUTE stmt;
大家讲道理2017-06-22 11:56:36
select name, sum(case when Course='Chinese' then Score end) as Chinese,
sum(case when type='Math' then Score end) as Math,
sum(case when type='English' then Score end) as English
from table1
group by name
PHP中文网2017-06-22 11:56:36
Anda harus mempelajari prinsipnya
Baidu "penukaran lajur baris mysql"
Tiada guna untuk hanya mempelajari SQL ini
扔个三星炸死你2017-06-22 11:56:36
Sql seperti dewa ini digunakan semasa melakukan statistik data Ia tidak digunakan dalam kerja PHP biasa.