我一直在尝试使用唯一行创建INSERT
,但是,如果唯一行已存在,则只会忽略该插入并且不会返回任何错误。< / p>
为什么,和/或查询会出现什么问题?
INSERT INTO hashtag_mapping (user_id, cid, hashtag_id, date_created, date_modified)
SELECT * FROM (SELECT 1, 8923, 1, NOW(), CURRENT_TIMESTAMP) AS tmp
WHERE NOT EXISTS (
SELECT user_id, cid, hashtag_id
FROM hashtag_mapping
WHERE user_id = 1
AND cid = 8923
AND hashtag_id = 1
) LIMIT 1;
唯一键:unique_mapping (user_id, cid, hashtag_id), Unique
我从MySQL收到以下错误:
列已存在:1060重复的列名&#39; 1&#39;
表设计是否有帮助...
id user_id cid hashtag_id date_created date_modified
------ ------- ---------- ---------- ------------------- ---------------------
1 1 8644 1 2016-03-23 15:19:54 2016-04-06 11:39:32
2 1 8644 2 2016-03-23 15:19:54 2016-04-06 11:39:34
3 1 8664 3 2016-03-25 17:02:32 2016-04-06 11:39:35
4 1 8664 4 2016-03-25 17:02:32 2016-04-06 11:39:36
答案 0 :(得分:4)
您必须为列提供别名。如果你不这样做,MySQL就会把Constant作为名字。
SELECT 1 AS field1 , 8923 AS something , 1 AS field2, NOW(), CURRENT_TIMESTAMP