rarebit Posted June 4, 2008 Share Posted June 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/ Share on other sites More sharing options...
Hooker Posted June 5, 2008 Share Posted June 5, 2008 Take a look into "LAST_INSERT_ID()" Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558094 Share on other sites More sharing options...
rarebit Posted June 5, 2008 Author Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558213 Share on other sites More sharing options...
luca200 Posted June 5, 2008 Share Posted June 5, 2008 Honestly I can't understand what you are trying to do.... Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558216 Share on other sites More sharing options...
rarebit Posted June 5, 2008 Author Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558221 Share on other sites More sharing options...
luca200 Posted June 5, 2008 Share Posted June 5, 2008 Honestly I can't understand what you are trying to do.... If you'd like me to understand something, you'd better try explaining it in english instead of pseudo code... Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558254 Share on other sites More sharing options...
fenway Posted June 5, 2008 Share Posted June 5, 2008 You can't get back all the new IDs from multi-valued insert statements. Quote Link to comment https://forums.phpfreaks.com/topic/108758-insert-select/#findComment-558320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.