mysql 將列值改變為列的辦法。本站提示廣大學習愛好者:(mysql 將列值改變為列的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql 將列值改變為列的辦法正文
-- 創立庫
CREATE TABLE `rate` (
`uname` VARCHAR (300),
`object` VARCHAR (300),
`score` VARCHAR (300)
);
-- 拔出數據
INSERT INTO test.rate (uname, object, score)
VALUES('aaa', 'chinese', '67'),
('aaa', 'math', '89'),
('aaa', 'physical', '89'),
('bbb', 'chinese', '67'),
('bbb', 'math', '75'),
('bbb', 'physical', '89');
-- 查詢
SELECT DISTINCT uname AS '姓名',
SUM(CASE object
WHEN 'chinese' THEN score END) AS '語文',
SUM(CASE object
WHEN 'math' THEN score END) AS '數學',
SUM(CASE object
WHEN 'physical' THEN score END) AS '物理' FROM rate GROUP BY uname;