我有两个表:这是客户表。
+--------+---------+--------+------------+--------------+------------+
| 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
的产品列。奇怪吧?