Combine multiple rows into one MySQL

后端 未结 2 545
臣服心动
臣服心动 2020-12-20 08:43

I need some help. I googled for solution, but I didn\'t found one.

I have three tables:

langs
(int) id | (varchar) language
    1    |       English
         


        
相关标签:
2条回答
  • 2020-12-20 09:18

    You are essentially looking at a pivot table. It Is doable, though the code is a bit cumbersome.

    one source: http://www.artfulsoftware.com/infotree/queries.php#78

    In the end though most any mysql solution will require either some wizardry with stored procedures or... hardcoding what languages you return.

    Pivot tables are one area that mysql doesn't shine in.

    0 讨论(0)
  • 2020-12-20 09:40
    SELECT CONCAT(
      ' SELECT `value`.`key_id`,'
    ,        '`keys`.`keys`,'
    ,         GROUP_CONCAT(
               'GROUP_CONCAT(IF(`value`.`lang_id`=',id,',`value`.`value`,NULL))'
              ,   ' AS `', REPLACE(language, '`', '``'), '`'
              )
    , ' FROM `keys` JOIN `value` ON `value`.`key_id` = `keys`.`id`'
    , ' GROUP BY `value`.`key_id`'
    )
    INTO @sql
    FROM `langs`;
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    0 讨论(0)
提交回复
热议问题