简体   繁体   中英

SQL script for merging two tables and turning rows into columns

I have two tables:

CREATE TABLE IF NOT EXISTS `tools` (
  `pk_id` int(11) NOT NULL AUTO_INCREMENT,
  `id` int(11) NOT NULL,
  `title` varchar(13) DEFAULT NULL,
  PRIMARY KEY (`pk_id`));

CREATE TABLE IF NOT EXISTS `features` (
  `pk_id` int(11) NOT NULL AUTO_INCREMENT,
  `id` int(11) NOT NULL,
  `feature` varchar(13) DEFAULT NULL,
  `number` int(11),
  PRIMARY KEY (`pk_id`),
  FOREIGN KEY (id) REFERENCES tools(id));

Then I have the script (see below) to obtain something like this:

Tools  Feature1 Feature2 Feature3 Feature4
=========================================
ToolA  1        0        0        1
ToolB  0        0        1        0

The script works fine when I put 1 instead of number in the line MAX(IF(pa1.feature = "', feature,'", "', number, '", "', 0, '")) . Otherwise it says there is some error at this line. Why and how to fix it?

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(pa1.feature = "', feature,'", "', number, '", "', 0, '")) AS ', REPLACE(feature, ' ', '')
    )
  ) INTO @sql
FROM features;

SET @sql = CONCAT('SELECT p.id
                    , p.title
                    , ', @sql, ' 
                   FROM tools p
                   LEFT JOIN features AS pa1 
                    ON p.id = pa1.id
                   GROUP BY p.id');

SELECT concat('SQL: ', @sql);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE:

This is the original SQL statement that was created by the above script and that contains some error:

SELECT p.id\\n , p.title\\n , MAX(IF(pa1.feature = \\"feature 68\\", \\"6\\", \\"0\\")) AS feature68,MAX(IF(pa1.feature = \\"feature 68\\", \\"7\\", \\"0\\")) AS feature68,MAX(IF(pa1.feature = \\"feature 68\\", \\"1\\", \\"0\\")) AS feature68,MAX(IF(pa1.feature = \\"feature 172\\", \\"2\\", \\"0\\")) AS feature172,MAX(IF(pa1.feature = \\"feature 56\\", \\"1\\", \\"0\\")) AS feature56,MAX(IF(pa1.feature = \\"feature 193\\", \\"4\\", \\"0\\")) AS feature193,MAX(IF(pa1.feature = \\"feature 71\\", \\"3\\", \\"0\\")) AS feature71,MAX(IF(pa1.feature = \\"feature 201\\", \\"2\\", \\"0\\")) AS feature201,MAX(IF(pa1.feature = \\"feature 80\\", \\"2\\", \\"0\\")) AS feature80,MAX(IF(pa1.feature = \\"feature 203\\", \\"5\\", \\"0\\")) AS feature203,MAX(IF(pa1.feature = \\"feature 82\\", \\"8\\", \\"0\\")) AS feature82,MAX(IF(pa1.feature = \\"feature 80\\", \\"9\\", \\"0\\")) AS feature80,MAX(IF(pa1.feature = \\"feature 82\\", \\"9\\", \\"0\\")) AS feature82,MAX(IF(pa1.feature = \\"feature 201\\", \\"5\\", \\"0\\")) AS feature201,MAX(IF(pa1.feature = \\"feature 80\\", \\"15\\", \\"0\\")) AS feature80,MAX(IF(pa1.feature = \\"feature 56\\", \\"3\\", \\"0\\")) AS feature56,MAX(I \\n FROM tools p\\n
LEFT JOIN features AS pa1 \\n ON p.id = pa1.id\\n
GROUP BY p.id

As I said the same code works if I substitute number by 1 .

I see two problems with your posted code.

  • In sql strings are usually specified using single quotes not double quotes. I'm not 100% with mysql so that platform might allow the code you have here.

  • look at the last field MAX(I \\n FROM tools This is clearly wrong.

This is probably one of two things. @SQL is to small or you have a null in the number column. If the problem is you have a null then this will fix would be to use this where in your select statement.

FROM features
WHERE number is not null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM