错误代码:1060。列名重复
Posted
技术标签:
【中文标题】错误代码:1060。列名重复【英文标题】:Error Code: 1060. Duplicate column name 【发布时间】:2016-08-04 01:48:19 【问题描述】:我一直收到错误代码:1060。:
重复的列名“NULL” 重复的列名“2016-08-04 01:25:06” 列名“john”重复但是,我需要插入一些具有相同值的字段,但 SQL 拒绝并显示上述错误。错误很可能是sql can't select the same column name,那样的话还有其他写代码的方法吗?以下是我当前的代码
INSERT INTO test.testTable SELECT *
FROM (SELECT NULL, 'hello', 'john', '2016-08-04 01:25:06', 'john'
, '2016-08-04 01:25:06', NULL, NULL) AS tmp
WHERE NOT EXISTS (SELECT * FROM test.testTable WHERE message= 'hello' AND created_by = 'john') LIMIT 1
我的专栏:
(id、消息、created_by、created_date、updated_by、updated_date、deleted_by、deleted_date)请帮忙,谢谢。
【问题讨论】:
【参考方案1】:您的重复列名来自您的子查询。您选择null
、john
和2016-08-04 01:25:06
多次。为您选择的列提供名称/别名:
INSERT INTO test.testTable
SELECT *
FROM (SELECT NULL as col1, 'hello' as col2,
'john' as col3, '2016-08-04 01:25:06' as col4,
'john' as col5, '2016-08-04 01:25:06' as col6,
NULL as col7, NULL as col8) AS tmp
WHERE NOT EXISTS (SELECT *
FROM test.testTable
WHERE message= 'hello' AND created_by = 'john')
LIMIT 1
不确定limit 1
在这里是否有用,您只选择了可能插入的一行。
【讨论】:
@nicker -- 乐于助人 @nicker 如何选择多次?我不明白。有人可以详细说明吗?谢谢【参考方案2】:您正在使用子查询。因为你不给列别名,mysql 必须为你选择别名——它会选择用于定义的公式。
您可以编写不带子查询的查询:
INSERT INTO test.testTable( . . .)
SELECT NULL, 'hello', 'john', '2016-08-04 01:25:06', 'john',
'2016-08-04 01:25:06', NULL, NULL
FROM dual
WHERE NOT EXISTS (SELECT 1
FROM test.testTable tt
WHERE tt.message = 'hello' AND tt.created_by = 'john'
);
如果您确实在SELECT
中使用了子查询,则在WHERE
子查询中使用相关子句:
INSERT INTO test.testTable( . . .)
SELECT *
FROM (SELECT NULL as col1, 'hello' as message, 'john' as created_by,
'2016-08-04 01:25:06' as date, 'john' as col2,
'2016-08-04 01:25:06' as col3, NULL as col4, NULL as col5
) t
WHERE NOT EXISTS (SELECT 1
FROM test.testTable tt
WHERE tt.message = t.message AND
tt.created_by = t.created_by
);
此外,LIMIT 1
没有做任何事情,因为您只有一行。
【讨论】:
以上是关于错误代码:1060。列名重复的主要内容,如果未能解决你的问题,请参考以下文章
Laravel Migrate:“列已存在”1060 列名重复
迁移时重复的列名'model_id'django mysql错误