aximbigfan Posted November 17, 2008 Share Posted November 17, 2008 Hi, I have a MySQL table with 3 colums. 1 is "dir" 2 is "key" and 3 is the actual data. I want to make an IF/Then system something like this (although this is not proper SQL, obviously) if (exists (where dir='123' AND where key='456') insert into... else update How would I do this? Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/ Share on other sites More sharing options...
corbin Posted November 17, 2008 Share Posted November 17, 2008 OMG MANS YOU HAD CAPS SO I SHOULD PROBABLY RESPOND QUICKLY!!!! Anyway, UPDATE INTO. Or two separate queries. Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-691845 Share on other sites More sharing options...
aximbigfan Posted November 17, 2008 Author Share Posted November 17, 2008 OMG MANS YOU HAD CAPS SO I SHOULD PROBABLY RESPOND QUICKLY!!!! Anyway, UPDATE INTO. Or two separate queries. I used caps because that's how I have seen SQL commands formatted, not because I need a quick answer. In fact, I am in no hurry. Thanks for your help, Chris Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-691850 Share on other sites More sharing options...
JasonLewis Posted November 17, 2008 Share Posted November 17, 2008 Haha, nice one corbin you silly fool. LOL. Why don't you just run separate queries? Run a query to check if it exists, if it does, insert, else update. Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-691852 Share on other sites More sharing options...
Mchl Posted November 17, 2008 Share Posted November 17, 2008 It's MySQL isn't it? If so, there's this useful syntax INSERT INTO table (column1, column2) VALUES (value1, value2) ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2... But you have to have a UNIQUE index in that table for that to work (note: PRIMARY is UNIQUE) Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-691899 Share on other sites More sharing options...
corbin Posted November 17, 2008 Share Posted November 17, 2008 OMG MANS YOU HAD CAPS SO I SHOULD PROBABLY RESPOND QUICKLY!!!! Anyway, UPDATE INTO. Or two separate queries. I used caps because that's how I have seen SQL commands formatted, not because I need a quick answer. In fact, I am in no hurry. Thanks for your help, Chris Haha, nice one corbin you silly fool. LOL. Why don't you just run separate queries? Run a query to check if it exists, if it does, insert, else update. It's the only all caps title on the page. Kinda stood out. Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-691998 Share on other sites More sharing options...
aximbigfan Posted November 17, 2008 Author Share Posted November 17, 2008 All: It looks like I will have to run 2 separate queries. I hate doing that, but it is the only way, I think. I attempted ON DUPLICATE KEY, but it doesn't work because it isn't a _full_ duplicate if the data is changed, but the "dir" and "key" columns are kept the same. Chris Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-692031 Share on other sites More sharing options...
Mchl Posted November 17, 2008 Share Posted November 17, 2008 Then create a unique key on dir, key columns Quote Link to comment https://forums.phpfreaks.com/topic/133023-mysql-if-exists-update-else-insert/#findComment-692071 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.