0

I have two tables.

CREATE TABLE `binnat` (
  `binNu` varchar(2) NOT NULL,
  `binNa` varchar(30) NOT NULL,
  `seq` char(2) NOT NULL,
  `se` tinyint(2) unsigned DEFAULT NULL COMMENT 'test sequence\n',
  `W_id` tinyint(2) unsigned NOT NULL,
  `W_lt_id` varchar(15) NOT NULL,
  `W_lt_lpt_id` smallint(4) unsigned NOT NULL,
  PRIMARY KEY (`binNu`,`binNa`,`W_id`,`W_lt_id`,`W_lt_lpt_id`),
  KEY `fk_binNaT_W1_idx` (`W_id`,`W_lt_id`,`W_lt_lpt_id`),
  CONSTRAINT `fk_binNaT_W1` FOREIGN KEY (`W_id`, `W_lt_id`, `W_lt_lpt_id`) REFERENCES `w` (`id`, `lt_id`, `lt_lpt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And

CREATE TABLE `binv` (
  `value` double DEFAULT NULL,
  `binNaT_binNu` varchar(2) NOT NULL,
  `binNaT_binNa` varchar(30) NOT NULL,
  `binNaT_W_id` tinyint(2) unsigned NOT NULL,
  `binNaT_W_lt_id` varchar(15) NOT NULL,
  `binNaT_W_lt_lpt_id` smallint(4) unsigned NOT NULL,
  PRIMARY KEY (`binNaT_binNu`,`binNaT_binNa`,`binNaT_W_id`,`binNaT_W_lt_id`,`binNaT_W_lt_lpt_id`),
  CONSTRAINT `fk_binV_binNaT1` FOREIGN KEY (`binNaT_binNu`, `binNaT_binNa`, `binNaT_W_id`, `binNaT_W_lt_id`, `binNaT_W_lt_lpt_id`) REFERENCES `binnat` (`binnu`, `binna`, `w_id`, `w_lt_id`, `w_lt_lpt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I made a query to retrive results. Here is it.

SELECT binnat.W_id, binnat.binna, binv.value
FROM binnat INNER JOIN binv ON (binnat.W_lt_id = binv.binNaT_W_lt_id) AND (binnat.W_id = binv.binNaT_W_id) AND (binnat.binNa = binv.binNaT_binNa) AND (binnat.binNu = binv.binNaT_binNu)
WHERE (((binv.binNaT_W_lt_id) Like "SI06840"))
GROUP BY binnat.se, binnat_W_id, binv.value, binnat.binnu, binnat.binna

When I execute it, I got this results.

W_id     binNa       value
1         abc          20
2         abc          30
1         def          70
2         def          50

But I found that the result was not what I wanted. The result I wanted was,

W_id     abc     def
1        20       70
2        30       50

I thought I needed codes to create column names dynamically from the query I made. And I found here code that could help me. That is look like,

SET @sql = null;
SELECT GROUP_CONCAT(DISTINCT
            CONCAT('MAX(IF(binnat.W_id = ', 
            W_id, 
            ', binnat.binNa, NULL)) AS `',
            binna,'`'
            )
            ) INTO @sql FROM `***mytable***`;

SET @sql = CONCAT('SELECT id, ', @sql, ' 
                  FROM ***mytable*** 
                  GROUP BY id');

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

But the code use a table in "FROM" phrase rather ther using a query. I'd like to know how I can use a query instead of 'mytable' in the above code.

Thanks.

  • It's not clear what you are asking. Why do you need to generate the column names dynamically if you know what the query results are already? – Nick Jan 12 '19 at 07:27
  • @Nick The query result I showed in the above should be the input of the code I wrote in the above code. That is to say, the query result should be mytable so as for me to get final result. In the final result, the column values(abc, def) will be the column names. – Jeung Hun Han Jan 12 '19 at 09:30
  • If you could add the query you are using to get those results to the question it would make it easier to answer. – Nick Jan 12 '19 at 09:33
  • @Nick Sorry I don't know how to add a query in this comment so that you can read easier. Anyway, the query that shows the result I showed in the above is like this,
    SELECT binnat.W_id, binnat.binNa, binnat.binnu, binv.value, binnat.se FROM binnat INNER JOIN binv ON (binnat.binNu = binv.binNaT_binNu) AND (binnat.binNa = binv.binNaT_binNa) AND (binnat.W_id = binv.binNaT_W_id) AND (binnat.W_lt_id = binv.binNaT_W_lt_id) WHERE (((binv.binNaT_W_lt_id) Like "proNum")) GROUP BY binnat.se, binnat_W_id, binv.value, binnat.binnu, binnat.binna;
    – Jeung Hun Han Jan 12 '19 at 10:12
  • @JeungHunHan Don't write any code inside a comment. Please [edit] your question to include this useful information to your question instead. Also please read https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query for SQL related questions and edit your question accordingly. – Progman Jan 12 '19 at 12:55
  • you are looking for pivot tables, which mysql doesn't do naturally. Check out this answer https://stackoverflow.com/questions/7674786/mysql-pivot-table – Garr Godfrey Jan 13 '19 at 02:06
  • @Garr Godfrey I am afraid that I explained about what I wanted. When I look at the link you mentioned, I couldn't find any amswere what I wanted. I asked how I can use my query as an input of the code in the above question. – Jeung Hun Han Jan 13 '19 at 08:12
  • 1
    You should be able to just replace `***mytable***` with your query in parens, if that's what you are asking. – Garr Godfrey Jan 14 '19 at 10:42

0 Answers0