MySQL使用虚拟列连接表

时间:2016-06-07 23:10:35

标签: mysql sql dynamic multiple-columns

我想制作虚拟列。在list id主表中,我需要使用'data'连接虚拟列。

Table: columns
+------+---------+-------------+
| [ID] | ID_USER |   DATENAME  |
+------+---------+-------------+
|   1  |   NULL  | Description |
+------+---------+-------------+
|   2  |   NULL  |     Cost    |
+------+---------+-------------+
|   3  |    2    |    Width    |
+------+---------+-------------+

Table: list
+----+-----------+------------+--------+
|[ID]|    NAME   |    DATE    |  COLOR |
+----+-----------+------------+--------+
|  1 |  234/2016 | 2016-06-06 |   red  |
+----+-----------+------------+--------+
|  2 | 1000/2016 | 2016-06-07 |  blue  |
+----+-----------+------------+--------+
|  3 | 3456/2016 | 2016-06-08 | yellow |
+----+-----------+------------+--------+


Table: data
+-----------+-----------+-------+
| ID_REPAIR | ID_COLUMN | VALUE |
+-----------+-----------+-------+
|     1     |     1     |  aaaa |
+-----------+-----------+-------+
|     1     |     2     |  10$  |
+-----------+-----------+-------+
|     2     |     1     |  bbbb |
+-----------+-----------+-------+
|     2     |     2     |  20$  |
+-----------+-----------+-------+
|     3     |     1     |  cccc |
+-----------+-----------+-------+
|     3     |     2     |  30$  |
+-----------+-----------+-------+


Result:
+------+-----------+------------+--------+-------------+------+
| [ID] |    NAME   |    DATE    |  COLOR | Description | Cost |
+------+-----------+------------+--------+-------------+------+
|   1  |  234/2016 | 2016-06-06 |   red  |     aaaa    |  10$ |
+------+-----------+------------+--------+-------------+------+
|   2  | 1000/2016 | 2016-06-07 |  blue  |     bbbb    |  20$ |
+------+-----------+------------+--------+-------------+------+
|   3  | 3456/2016 | 2016-06-08 | yellow |     cccc    |  30$ |
+------+-----------+------------+--------+-------------+------+

在此查询中,我得到名称columns

SELECT * FROM `columns` WHERE `id_user` IS NULL

并且在PHP中将ID保存到变量,但是在测试中我希望生成只有第二列的表。在下面的代码中,我想生成带有Cost列的结果,但始终是插入Description列:

SQL:

SELECT `list`.`id`, `name`, `date`, `color`, `data`.`value`
FROM `list` 
INNER JOIN `data` ON `list`.`id` = `data`.`id_repair` WHERE `repair_data`.`id_column` = 2

我不知道INNER JOIN如何仅WHERE id_column等于2

1 个答案:

答案 0 :(得分:2)

SELECT返回的列数无法在执行时动态确定。必须在查询的SELECT列表中指定列数以及分配给每列的数据类型和名称(或别名)。让我们从那开始吧。

要让SQL语句返回结果集,如图所示,查询必须采用以下格式:

 SELECT l.id
      , l.name
      , l.date
      , l.color
      , (expr1)     AS `Description`
      , (expr2)     AS `Cost`
   FROM list l
    ... 

返回六个列。

对于用于返回expr1expr2列的表达式DescriptionCost,有几种方法可以展开实体属性值(EAV)模型回到规范关系模型。

最容易理解,但不一定是最佳选择,是在SELECT列表中使用相关子查询。举个例子:

 SELECT l.id
      , l.name
      , l.date
      , l.color
      , ( SELECT d.value
            FROM `data` d
           WHERE d.id_repair = l.id
             AND d.id_column = 1
           ORDER BY d.value
           LIMIT 1
        )                      AS `Description`
      , ( SELECT c.value
            FROM `data` c
           WHERE c.id_repair = l.id
             AND c.id_column = 2
           ORDER BY c.value
           LIMIT 1
        )                      AS `Cost`
      , ( SELECT w.value
            FROM `data` w
           WHERE w.id_repair = l.id
             AND w.id_column = 3
           ORDER BY w.value
           LIMIT 1
        )                      AS `Width`
   FROM list l
  ORDER BY l.id

请注意,SELECT列表中的相关子查询只能返回一行,并返回单个表达式。也就是说,它返回单个值。

作为替代方案,我们可以使用外连接操作和条件聚合。例如:

 SELECT l.id
      , l.name
      , l.date
      , l.color
      , MAX(IF(d.id_column=1,d.value,NULL)) AS `Description`
      , MAX(IF(d.id_column=2,d.value,NULL)) AS `Cost`
      , MAX(IF(d.id_column=3,d.value,NULL)) AS `Width`
   FROM list l
   LEFT
   JOIN data d
     ON d.id_repair = l.id 
  GROUP BY l.id, l.name, l.date, l.color

如果我们需要根据存储在数据库中的表中的信息,使用不同数量的列和用于列的表达式来生成返回的结果 dynamic ,那么我们可以先获取来自数据库的信息,然后使用它来帮助我们构造我们需要执行的实际SQL语句以获得最终结果。

或者,正如许多使用实体属性值(EAV)模型的应用程序那样,我们甚至不会尝试将EAV模型强制回到规范的关系模型中。我们只是让应用程序运行从表中检索信息所需的多个查询。基本上与应用程序如何将行插入表中相反。