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
https://forums.phpfreaks.com/topic/108758-insert-select/
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
https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558213
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
https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558221
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.