Mysql
 sql >> Database >  >> RDS >> Mysql

Trasformazione da riga a colonna da tabelle diverse e numero di righe diverso (unione) in MySQL versione 8.0.17 utilizzando Pivot

In sostanza vuoi un full join , che MySQL non supporta. Un metodo per gestirlo è union all e aggregazione:

SELECT MAX(CASE WHEN which = 'Q400' THEN contents END) as Q400,
       MAX(CASE WHEN which = 'Q401' THEN contents END) as Q401       
c2.contents Q410
FROM ((SELECT 'Q400' as which, c.*,
              ROW_NUMBER() OVER (ORDER BY sID) as seqnum
       FROM t_contents_q400 c
      ) UNION ALL
      (SELECT 'Q401' as which, c.*,
              ROW_NUMBER() OVER (ORDER BY sID) as seqnum
       FROM t_contents_q401 c
      )
     ) q
GROUP BY seqnum;