我想製作虛擬列。在list
id主表中,我需要將虛擬列與'data'結合在一起。MySQL使用虛擬列加入表格
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只有在id_column等於2