Jump to content

Insert ... Select


rarebit

Recommended Posts

Hi, I seem to be struggling with this one quite a bit...

I have a table with a unique field, as I insert (whether it duplicates or not) I need to get it's id.

 

$s = "INSERT INTO test VALUES ('', 'joe') ON DUPLICATE KEY UPDATE name = 'joe', SELECT id, name FROM test WHERE name = 'joe' ";
$s = "INSERT INTO test VALUES ('', 'joe'), SELECT id, name FROM test WHERE name = 'joe' ";

 

I assumed it went something like that but it's not working out. Where am I going wrong?

 

Is it also possible to have numerous of these statements in a single query?

 

Link to comment
Share on other sites

That's what i'm currently using, but I have 4 of these statements and i'm after combining them into a single call, getting all those id's at once. Then is possible to insert into a new entry of another table.

 

It's meant to be a memory saving way of logging, but it seems mysql intensive. However if I was to go back to the Access style method of joining or lookups. Only here the lookups are dynamic...

 

It's currently not critical (just mysql wasting), so when I have time i'll rephrase the Q and give a full example. However if someone does have avenue towards a solution then it'll be greatly appreciated...

 

wobbly

Link to comment
Share on other sites

INSERT agent SELECT id_1 (ON DUPLICATE)
INSERT link SELECT id_2 (ON DUPLICATE)
INSERT sess SELECT id_3 (ON DUPLICATE)
INSERT response SELECT id_4 (ON DUPLICATE)
INSERT ip SELECT id_5 (ON DUPLICATE)

INSERT ('', $time, $method, id_1, id_2, id_3, id_4, id_5)

 

Thats a very basic pseudo code of what happens now, but i'm after putting it all into a single call...

 

*** the on duplicate probably isn't required, but is in the examples of INSERT...SELECT that seem right for me, but i'm not sure if my intended use is different.

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.