drqn5418 2013-11-26 09:33
浏览 43
已采纳

动态列名称取决于查询结果的单行

I need some help with dynamical column names in a sql query. First I will try to explain my database structure and then the problem.

Database structure:

admin_group table:

+--------+----------------+
| id     | language_code  |
+--------+----------------+
| 1      | en_UK          | 
| 2      | de_DE          | 
| 3      | es_ES          |
+--------+----------------+

constructions_meta table:

+--------+-----------------+----------+
| id     | admin_group_FK  | value    |
+--------+-----------------+----------+
| 1      | 1               | 0.13     |
| 2      | 2               | 0.12     |
| 3      | 3               | 0.10     |
+--------+-----------------+----------+

construction_lang table:

+--------+-----------------+----------------+----------------+
| id     | en_UK_name      | de_DE_name     |es_ES_name      |
+--------+-----------------+----------------+----------------+
| 1      | Construction 1  | Konstruktion 1 | Construcción 1 |       
| 2      | Construction 2  | Konstruktion 2 | Construcción 2 |  
| 3      | Construction 3  | Konstruktion 3 | Construcción 3 |  
+--------+-----------------+----------------+----------------+

Those are my tables in the database. What I need here is to get the names of the constructions regarding the language code for each construction. For example I want to list the constructions as following:

  1. Construction 1
  2. Konstruktion 2
  3. Construcción 3
  • 写回答

3条回答 默认 最新

  • dsfsad089111 2013-11-26 09:44
    关注

    It's probably easiest to do that in two passes. That'll give you the ability to expand the width of the construction_lang table without affecting the SQL statements significantly.

    Firstly you issue a SQL statement to get the column names that you need, then you use that result set to build a second SQL statement that will get the names for you.

    This is not the ideal solution, as it is working around the data-model.

    Given a query that returns back the id and language from admin_group in line with:

    array( array( 'id' => 1, 'language_code' => 'en_UK' ),
           array( 'id' => 2, 'language_code' => 'de_DE' ),
           array( 'id' => 3, 'language_code' => 'es_ES' ) )
    

    You can build a statement with something along the lines of (using the other answer from diarmuid as an example)

    $sql = "select c.id , (select case c.admin_group_FK
    ";
    foreach( $languages as $thisLanguage ) {
       $sql .= "when {$thisLanguage['id']} then l.${thisLanguage['language_code']}_name
    ";
    }
    $sql .= ...
    

    This is a round-about way of doing it, because of the data-model you have. Ideally you wouldn't need the "dynamic" SQL."

    If you want to remove the round-about-ness, and build something more in line with standard relational database theory, then you can change the model so that the construction_lang table is more like this:

    +------------------------+-----------------+----------------+
    | constructions_meta_fk  | language        | name           |
    +------------------------+-----------------+----------------+
    | 1                      | en_UK           | Construction 1 |
    | 1                      | de_DE           | Konstruktion 1 |
    | 1                      | es_ES           | Construcción 1 |
    | 2                      | en_UK           | Construction 2 |
    | 2                      | de_DE           | Konstruktion 2 |
    | 2                      | es_ES           | Construcción 2 |
    | 3                      | en_UK           | Construction 3 |
    | 3                      | de_DE           | Konstruktion 3 |
    | 3                      | es_ES           | Construcción 3 |
    +------------------------+-----------------+----------------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式