Tuesday, March 1, 2011

MySQL - A difficult INSERT...SELECT on the same table [MySQL 5.051]

I am trying to insert a new row into my table which holds the same data as the one I am trying to select from the same table but with a different user_id and without a fixed value for auto_id since that is an auto_increment field, and setting ti to NOW(). Below is my mockup query where '1' is the new user_id. I have been trying many variations but am still stuck, anyone who can help me with turning this into a working query.

INSERT INTO `lins` ( `user_id` , `ad` , `ke` , `se` , `la` , `ra` , `ty` , `en` , `si` , `mo` , `ti` , `de` , `re` , `ti` ) (

SELECT '1', `ad` , `ke` , `se` , `la` , `ra` , `ty` , `en` , `si` , `mo` , `ti` , `de` , `re` , NOW( )
FROM `lins`
WHERE autoid = '4'
AND user_id = '2'
)

Thank you for taking the time to help me out!

From stackoverflow
  • I just tried it, and it works for me.

    Did you misspell the column name corresponding to "ke"?

    I'm guessing the actual column names in your table are not just two letters. E.g. you have two columns named "ti" in the query you show, so I assume you have edited these down from longer names.

  • Presumably you know that "WHERE autoid = 4" is sufficient if it's a unique autoid. And if you intended to single-quote the integers (1, 4 and 2), and they're numeric, you've created implied casts; in the WHERE clause, that will disable the ability of the optimizer to use indexes on the resulting integer values.

    Also, using unnecessary (and easily mistyped) back-ticks has been the root of at least one other similar question here.

0 comments:

Post a Comment