试图将table1中的一列插入table2中的现有列?

时间:2014-06-22 21:40:30

标签: php mysql sql

我有两个表:这是客户表。

+--------+---------+--------+------------+--------------+------------+
|  id    |  name   | debt   | phone      | address      |  products  |
+--------+---------+--------+------------+--------------+------------+
|  1     | person1 | 55.54  | 8187654545 | Planet Earth | (none yet) |
|  1     | person2 | 55.64  | 8184256595 | Planet Mars  | (none yet) |
+--------+---------+--------+------------+--------------+------------+

这是产品表:

+-----+----------+----------+----------+----------------+-------+
|  id | category |   name   |   type   | size_in_ounces | price |
+-----+----------+----------+----------+----------------+-------+
|   1 | liquids  | product1 | original | 2.7            | 1.99  |
|   1 | liquids  | product2 | original | 7.7            | 5.99  |
|   1 | liquids  | product3 | original | 10             | 7.99  |
+-----+----------+----------+----------+----------------+-------+

我正在尝试将name表格中的products列添加到products表格中的clients列。基本上我想要完成的是,在查询时,这将显示在clients表中,如果可能,clients debt列会相应地增加价格。

+--------+---------+--------+------------+--------------+------------+
|  id    |  name   | debt   | phone      | address      |  products  |
+--------+---------+--------+------------+--------------+------------+
|  1     | person1 | 63.49  | 8187654545 | Planet Earth | product3   |
|  1     | person2 | 57.63  | 8184256595 | Planet Mars  | product1   |
+--------+---------+--------+------------+--------------+------------+

我尝试这样做:但SQL给了我一个错误。我知道以下代码是错误的,但它可以让你知道我想要完成的任务。

INSERT INTO `clients` (products) WHERE `name` = "person1"
SELECT `name` FROM `products` WHERE `name` = "product3";

这是另一个愚蠢的事情;如果我这样做代码:

INSERT INTO `clients` (products)
SELECT `name` FROM `products` WHERE `name` = "product3";

它创建了一个全新的客户端,每个列都为null或为空,除了它添加product3的产品列。奇怪吧?

0 个答案:

没有答案