扩展相同ID的Mysql表

时间:2013-10-30 14:46:12

标签: mysql

我的表结构是:

id | type | attribute | customer_id | value

1 | 2 | 1 | 1 | some
2 | 2 | 2 | 1 | this
3 | 2 | 3 | 1 | that
4 | 2 | 1 | 2 | cool
5 | 2 | 2 | 2 | just

我想为每个 customer_id 添加 ='mine'作为属性 4。

INSERT INTO mytable 
SET type='2', attribute='4, value='mine'

问题是如何在customer_id上绑定它,每个客户只绑定一次?

1 个答案:

答案 0 :(得分:1)

INSERT INTO myTable(type, attribute, customer_id, value)
SELECT  2 type,
        4 attribute,
        s.customer_id,
        'mine' `value`
FROM    (SELECT DISTINCT customer_id FROM myTable) s