为什么在mysql中透视表时我的值显示为null

0yg35tkg  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(300)

我正在使用mysql-rows-to-columns和本教程http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/#comment-6128帮助我旋转一张table,效果很好。从这个开始:

mediaID q_short_name    start_time  stop_time   audio_link
ee      CVV Number       208            210     j.mp3
ee      Expiration Date  308            310     j.mp3
ff      CVV Number       124            127     k.mp3
ff      Expiration Date  166            169     k.mp3

目标是:

mediaID CVVNumStartT CVVNumStopT ExpDateStart_time ExpDateStop_time Aud
ee      208          210         308               310            k.mp3
ff      124          127         166               169            j.mp3

我有一部分是通过这个代码实现的:

CREATE VIEW test__extension AS (
SELECT amr_text.*, 
CASE WHEN q_short_name = 'CVV Number' THEN amr_text.start_time END AS 
CVV_Start_Time,
CASE WHEN q_short_name = 'CVV Number' THEN amr_text.stop_time END AS 
CVV_Stop_Time,
CASE WHEN q_short_name = 'Expiration Date' THEN amr_text.start_time END 
AS Expiration_Start_Time,
CASE WHEN q_short_name = 'Expiration Date' THEN amr_text.stop_time END 
AS Expiration_Stop_Time, FROM amr_text);
 CREATE VIEW test_extension_pivot AS (SELECT mediaID,
SUM(CVV_Start_Time) AS CVV_Start_Time,
SUM(CVV_Stop_Time) AS CVV_Stop_Time,
SUM(Expiration_Start_Time) AS Expiration_Start_Time,
SUM(Expiration_Stop_Time) AS Expiration_Stop_Time,
FROM test_extension GROUP BY mediaID);

这将创建与目标表完全相同的列。但是现在除了mediaid之外的所有值都呈现为null。我的问题是,为什么它们被null替换,我可以用什么代替sum来呈现expiration和cvv start和stop time的值,就像它们在原始表中一样?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题