Fog Juice Posted June 22, 2009 Share Posted June 22, 2009 I have the following query but I'm not sure of the exact syntax to use, this is what I want: IF NOT EXISTS(SELECT id FROM games_linked WHERE game_id = '$gid') BEGIN INSERT INTO `games_linked` (`id`,`server_id`,`game_id`) VALUES (NULL,'$serverid','$gid') END ELSE BEGIN UPDATE `games_linked` SET `server_id` = '$serverid' WHERE `game_id` = '$gid' END Does anyone have any suggestions for the correct syntax to make this work? Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/ Share on other sites More sharing options...
Ken2k7 Posted June 22, 2009 Share Posted June 22, 2009 Well if you set game_id to be the PK or UK, then try this - INSERT INTO games_linked (id, server_id, game_id) VALUES (NULL, $serverid, $gid) ON DUPLICATE KEY UPDATE server_id = $serverid; Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861052 Share on other sites More sharing options...
Fog Juice Posted June 22, 2009 Author Share Posted June 22, 2009 Why was this moved to PHP help? Clearly this is not a PHP question. Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861070 Share on other sites More sharing options...
Zane Posted June 22, 2009 Share Posted June 22, 2009 $connect = mysql_connect($domain, $user, $pass); mysql_select_db("yeoledatabase'); $serverid = 1; $gid = 575; $test = mysql_query("SELECT COUNT(*) as x, id FROM games_linked WHERE game_id = $gid") or die(mysql_error()); $result = mysql_fetch_array($test); if($result[0] == 0) { echo "Appending to Database"; $sql = "INSERT INTO `games_linked` (`server_id`,`game_id`) VALUES ($serverid,$gid)"; } else { echo "Updating"; $sql = "UPDATE `games_linked` SET `server_id` = $serverid WHERE `game_id` = $gid"; } $query = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861072 Share on other sites More sharing options...
Fog Juice Posted June 22, 2009 Author Share Posted June 22, 2009 $connect = mysql_connect($domain, $user, $pass); mysql_select_db("yeoledatabase'); $serverid = 1; $gid = 575; $test = mysql_query("SELECT COUNT(*) as x, id FROM games_linked WHERE game_id = $gid") or die(mysql_error()); $result = mysql_fetch_array($test); if($result[0] == 0) { echo "Appending to Database"; $sql = "INSERT INTO `games_linked` (`server_id`,`game_id`) VALUES ($serverid,$gid)"; } else { echo "Updating"; $sql = "UPDATE `games_linked` SET `server_id` = $serverid WHERE `game_id` = $gid"; } $query = mysql_query($sql) or die(mysql_error()); Thanks but I was looking for an answer native to MySQL, that is why it was in the MySQL section. I want to avoid having multiple SQL queries . I'm already using PHP atm for this, $return = mysql_query("UPDATE `games_linked` SET `server_id` = '$serverid' WHERE `game_id` = '$gid'"); if(mysql_affected_rows() == 0) { mysql_query("INSERT INTO `games_linked` (`id`,`server_id`,`game_id`) VALUES (NULL,'$serverid','$gid')") or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861075 Share on other sites More sharing options...
Zane Posted June 22, 2009 Share Posted June 22, 2009 Thanks but I was looking for an answer native to MySQL, that is why it was in the MySQL section. I want to avoid having multiple SQL queries Well pardon me, I guess I won't write you anymore free code examples either. Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861076 Share on other sites More sharing options...
Fog Juice Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks but I was looking for an answer native to MySQL, that is why it was in the MySQL section. I want to avoid having multiple SQL queries Well pardon me, I guess I won't write you anymore free code examples either. I'm *edit* not *edit(my bad,forgot to say not.. lol)* looking for an argument but it just frustrated me to log in and see my post was moved with no explanation or anything. You can't expect users to know your intentions without warning them first. I appreciate your help but my first knee jerk reaction was "wth was that moved for", now it makes sense but like I said, you can't expect users to to know your intentions unless you tell them. Anyways thanks for trying to help in the first place, this forum is an excellent resource and is great for ppl like me who are out of college but still require a little bit of help every now and then. Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861077 Share on other sites More sharing options...
Zane Posted June 22, 2009 Share Posted June 22, 2009 well, back to your question then.. syntax of what...SQL? I assumed what you wrote in the op post was pseudo code. It didn't appear to me as any SQL I've dabbled in. Perhaps someone else has used the functions BEGIN or END http://forums.mysql.com/read.php?10,251666,251670#msg-251670 Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861083 Share on other sites More sharing options...
corbin Posted June 22, 2009 Share Posted June 22, 2009 You could of course use UPDATE INTO, but there are downsides to that... It's only native to MySQL is pretty much the downside. You could technically do it in 1 function call if you used PDO to do multiple statements, but mysql_query can't do multiple statements. The PDO way wouldn't be any better than two mysql_query calls though, and the mysql_query way would probably be better. Any reason why you want to do it natively in MySQL? Quote Link to comment https://forums.phpfreaks.com/topic/163194-proper-syntax-for-if-not-exists-insert-else-update-query/#findComment-861088 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.