转换MySql子查询

时间:2016-07-12 09:01:56

标签: mysql

我希望mysql以特殊方式连接2个表。

以下是我的2个表格:

版权

  

right_id    thumbnail    enabled
   1 img1.png 1
   2 img2.png 1
   3 img3.png 1

右弦

  

right_id    language    string_key    string value
   1    en    short_header    Right 1
   1    de    short_header    Recht 1
   1    de    long_header    Langer Text
   1    en    long_header    Long Text
   1    en    content    Bla Bla Bla
   1    de    content    Bla Bla Bla

这也适用于其他语言和所有权利。

是否可以创建一个查询,该查询只返回以下格式的一行所有权限?

  

right_id thumbnail enabled short_header_en short_header_de long_header_en long_header_de content_en < KBD> content_de
   1 img1.png 1 Right 1 Recht 1 Long Text Langer Text Bla Bla Bla { {1}}

我认为mysql对子查询有一些变换,但我不知道甚至要查找什么。

谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用以下数据透视查询:

SELECT t1.right_id, t1.thumbnail, t1.enabled,
    MAX(CASE WHEN language = 'en' AND string_key = 'short_header' THEN string_value ELSE NULL) AS short_header_en,
    MAX(CASE WHEN language = 'de' AND string_key = 'short_header' THEN string_value ELSE NULL) AS short_header_de,
    MAX(CASE WHEN language = 'en' AND string_key = 'long_header'  THEN string_value ELSE NULL) AS long_header_en,
    MAX(CASE WHEN language = 'de' AND string_key = 'long_header'  THEN string_value ELSE NULL) AS long_header_de,
    MAX(CASE WHEN language = 'en' AND string_key = 'content'      THEN string_value ELSE NULL) AS content_en,
    MAX(CASE WHEN language = 'de' AND string_key = 'content'      THEN string_value ELSE NULL) AS content_de
FROM Rights t1 INNER JOIN RightsStrings t2
    ON t1.right_id = t2.right_id
GROUP BY t1.right_id

答案 1 :(得分:2)

这似乎是一个表格枢轴问题,不确定它是否是你想要的,但是尝试一下。

SELECT
    r.right_id, r.thumbnail, r.enabled,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'short_header_en' THEN string_value END) AS `short_header_en`,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'short_header_de' THEN string_value END) AS `short_header_de`,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'long_header_en' THEN string_value END) AS `long_header_en`,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'long_header_de' THEN string_value END) AS `long_header_de`,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'content_en' THEN string_value END) AS `content_en`,
    MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = 'content_de' THEN string_value END) AS `content_de`
FROM rights r
INNER JOIN right_strings rs
ON r.right_id = rs.right_id
GROUP BY r.right_id

对于动态表格枢轴:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(CASE WHEN CONCAT(rs.string_key, '_', rs.`language`) = ''',
      string_key, '_', `language`
      ''' THEN rs.string_value END) AS ',
      string_key, '_', `language`
    )
  ) INTO @sql
FROM right_strings;

SET @sql = CONCAT(
    'SELECT r.right_id, r.thumbnail, r.enabled, ',
    @sql,
    ' FROM rights r
    INNER JOIN right_strings rs
    ON r.right_id = rs.right_id
    GROUP BY r.right_id;');

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