时间:2016-04-19 13:36:24

标签: php mysql sql

假设我有3个表,每个表的结构与此类似:

| id | customer_id | col1       | col2       | col3        |
|----|-------------|------------|------------|-------------|
| 1  | C100        | some value | some value | some values |
| 2  | C101        |            |            |             |
| 3  | C102        |            |            |             |

现在我想用特定的customer-id复制行。

所以在伪代码中就像:

DUPLICATE FROM tab1, tab2, tab3 WHERE customer_ID = C100 SET customer_ID = C987;

customer_idC100的那3个表格中的值,并在每个表格中添加另一个条目,但使用新的customer_id C987。< / p>

3个表格如下:

| id | customer_id | col1       | col2       | col3        |
|----|-------------|------------|------------|-------------|
| 1  | C100        | some value | some value | some values |
| 2  | C101        |            |            |             |
| 3  | C102        |            |            |             |
| 4  | C987        | some value | some value | some value  |

此外,表中的结构略有不同。

id是主键,customer_id是唯一的。

1 个答案:

答案 0 :(得分:3)

也许你可以做一个insert-select:

INSERT INTO tab1
SELECT id, 'C987', col1, col2, col3
FROM   tab1
WHERE  customer_id = 'C100';

您可以对tab2和tab3执行类似的查询。

相关问题