在mysql中使用自连接透视表

cvxl0en2  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(355)

我需要像这样旋转一张table:

mediaID q_short_name start_time stop_time audio
 ee     A            208      210           j.mp3
 ee     B            308      310           j.mp3
 ff     A            124      127           k.mp3
 ff     B            166      169           k.mp3
 gg     C            582      584           p.mp3
 gg     B            489      492           p.mp3
 hh     D            222      225           q.mp3
 hh     C            121      124           q.mp3

对此:

mediaID Astart Astop Bstart Bstop Cstart Cstop Dstart Dstop audio
ee      208    210  308     310                              k.mp3
ff      124    127  166     169                              j.mp3
gg                   489    492    582   584                 p.mp3
hh                                 121   124    222    225   q.mp3

在前面的一个问题中,@rick james的以下代码说明了如何使用model在mysql中透视表,但我需要扩展它:

SELECT  c.mediaID,
    c.start_time AS CVVstart,
    c.end_time   AS CVVstop,
    e.start_time AS ExpStart,
    e.stop_time AS ExpStop,
    c.audio_file
FROM  my_test AS c
JOIN  my_test AS e  USING(mediaID)
WHERE  c.q_short_name = 'A'
  AND  e.q_short_name = 'B';

根据下面的解决方案,我尝试了这个方法(我现在添加的是实际的列名,而不是示例中的缩写字母):

SELECT DISTINCT 
 O.mediaID, 
 O.start_time AS CVVStart,
 O.stop_time AS CVVStop, 
 O.start_time AS ExpStart, 
 O.stop_time AS ExpStop,
 O.start_time AS CredCardCVVStart, 
 O.stop_time AS CredCardCVVStop,
 O.start_time AS CredCardNumStart,
 O.stop_time AS CredCArdNumStop, O.audio_link FROM my_test
 AS O
 LEFT JOIN my_test AS CVVStart
 ON CVVStart.mediaID = O.mediaID
  AND CVVStart.q_short_name = 'CVV Number - Start and Stop Time'

LEFT JOIN my_test AS CVVStop
ON CVVStop.mediaID = O.mediaID
AND CVVStart.q_short_name = 'CVV Number - Start and Stop Time'

LEFT JOIN my_test AS ExpStart
ON ExpStart.mediaID = O.mediaID
AND ExpStart.q_short_name = 'Expiration Date - Start and Stop Time'

LEFT JOIN my_test AS ExpStop
ON ExpStop.mediaID = O.mediaID
AND ExpStop.q_short_name = 'Expiration Date - Start and Stop Time'

LEFT JOIN my_test AS CredCardCVVStart
ON CredCardCVVStart.mediaID = O.mediaID
AND CredCardCVVStart.q_short_name = 'Credit Card CVV - Start and Stop 
Time'

LEFT JOIN my_test AS CredCardCVVStop
ON CredCardCVVStop.mediaID = O.mediaID
AND CredCardCVVStop.q_short_name = 'Credit Card CVV - Start and Stop 
Time'

LEFT JOIN my_test AS CredCardNumStart
ON CredCardNumStart.mediaID = O.mediaID
AND CredCardNumStart.q_short_name = 'Credit Card Number - Start and Stop 
Time';

我一定是做错了什么,因为我没有得到不同的媒体ID,而且每个单元格都被填充,而其中一些应该是空的。e、 g在第一行中,我得到308和310的重复值,这应该是一个的值,在这里是'b'或过期日期。如果有人能指出我在逻辑上的错误,我将不胜感激。

1rhkuytd

1rhkuytd1#

您可以通过以下查询获得此结果:

SELECT
  O.mediaID,
  -- start and stop for needed short_name here
  O.audio
FROM (
  SELECT DISTINCT mediaID, audio FROM my_test
) AS O
  LEFT JOIN my_test AS A
  ON A.mediaID = O.mediaID
  AND A.q_short_name = 'A'

  LEFT JOIN my_test AS B
  ON B.mediaID = O.mediaID
  AND B.q_short_name = 'B'

  LEFT JOIN my_test AS C
  ON C.mediaID = O.mediaID
  AND C.q_short_name = 'C'

在这里,我们得到不同的媒体ID,然后加入这个表中的任何 q_short_name

相关问题