sql >> Database >  >> RDS >> Mysql

MySQL-draaitabel met java

U kunt dynamische SQL gebruiken om dynamisch te draaien, zoals

SET @sql = NULL;
SET @date = '2020-11-30';

SELECT GROUP_CONCAT(
             CONCAT(
                    'SUM(CASE WHEN Partners = "', Partners,'" THEN Amount ELSE 0 END ) AS'
                    ,Partners
                    )
       )
  INTO @sql
  FROM ( SELECT DISTINCT Partners FROM BPFinal WHERE Date = @date ) AS b;

SET @sql = CONCAT('SELECT Branch,',@sql,
                   ' FROM BPFinal
                    WHERE Date = "',@date,'"' 
                  ' GROUP BY Branch'); 
                  
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt; 

Demo



  1. Is er een FIND_IN_SET op index in MySQL?

  2. MariaDB Backup &PostgreSQL in de cloud - ClusterControl Release 1.6.1

  3. gem install pg --with-pg-config werkt, bundel mislukt

  4. gebruik mysql ingebed met qt?