Search code examples
mysqlmariadbmysql-error-1064

MySQL - Column already exists: 1060 Duplicate column name '1'


I've been trying to make a INSERT with unique rows, however, if the unique row already exist, it will simply ignore the insert and return no error.

Why, and/or what would be wrong with the query?

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;

The unique key: unique_mapping (user_id, cid, hashtag_id), Unique

The following error that I receive from MySQL:

Column already exists: 1060 Duplicate column name '1'

And the table design if it's helps...

    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

Solution

  • You must give a alias for the columns. if you dont MySQL will take the Constant as name.

    SELECT 1 AS field1 , 8923 AS something , 1 AS field2, NOW(), CURRENT_TIMESTAMP