Jump to content

SELECT INSERT SQL Statement


Miko

Recommended Posts

Hello,

 

I'm trying to make an SQL statement that insert data when it isn't found with a SELECT, and this in 1 query

 

I have this for the moment:

 

INSERT INTO checkedimage
(quality, date, depot, tour, image_name, year, month, day, name_ok) 
SELECT * FROM checkedimage WHERE image_name NOT IN
(SELECT * FROM checkedimage), VALUES('y', 20091019, 0530, 'T002', '03-RUDI VAN LAER')

 

I'm doing wrong here, but don't know what :(

Link to comment
Share on other sites

NOT IN requires that subselect returns one column only. Also it's not the most efficent way of doing this, but we can deal with this later.

 

You also need to remove VALUES() part, as you're using INSERT...SELECT

Link to comment
Share on other sites

I'm not entirely sure on the objective, but I don't believe this can be done in one query, at least not in the manner you appear to be attempting. You will probably have to first run a SELECT query to fetch any rows that match your criteria and if it doesn't find any rows then run an INSERT query. Alternatively it's possible that you could make use of the INSERT ON DUPLICATEsyndax, but I think in your situation this is unlikely, as it will update the column if it already exists whereas I believe you wish to not update.

 

Edit: Well they say you learn something every day. You may also be able to use something along the lines of...

 

INSERT IGNORE INTO...

 

Which will just not insert if a key field will be duplicated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.