Databases :: MySql :: IF statement |
|||
| By: VB guy |
Date: 08/07/2003 00:00:00 |
Points: 25 | Status: Answered Quality : Excellent |
|
Hi, I guess this is a simple question, but I can't get it to word ;) Here is what I want: If the query "SELECT * FROM commands WHERE command= 'command'" returns something it should be updated with "UPDATE SET used= used+1 WHERE command= 'command'", but if it returns nothing it should use "INSERT INTO commands(used, command) VALUES('1', 'command')". Now I got this far: IF ((SELECT command FROM commands WHERE command= 'command') = 'command') { UPDATE SET used= used+1 WHERE command= 'command'; } else { INSERT INTO commands(used, command) VALUES('1', 'command'); } but this ain't working.. Anybody? |
|||
| By: VGR | Date: 08/07/2003 04:44:00 | Type : Comment |
|
| in which front-end language ? in PHP : // perform select via $result=mysql_query('SELECT ...') or die('bad query : '.mysql_error()); if (mysql_numrows($result)>0) { // results returned // perform UPDATE } else { // no results // perform INSERT } // if results NB : have a look at REPLACE INTO ;-) |
|||
| By: Squibi | Date: 08/07/2003 06:28:00 | Type : Comment |
|
| in Visual Basic, but it possible to perform an IF in MySql can't I? |
|||
| By: VGR | Date: 08/07/2003 08:02:00 | Type : Answer |
|
| excuse-me ? MySql, as any SQL RDBMS, has an "IF" function/statement, but SQL is NOT a programming language. For instance, it has no repetition structure per se (for..., while...do, repeat...until, foreach..., etc) to perform complicated actions, you need a "front-end program", and it happens one of the simpliest is PHP. In VB, do it exactly like I wrote it for PHP. Just call the relevant APIs to perform the desired operations (be it PrapereSql, SqlExecute, Sql.NextRecord() or the like) |
|||
| By: Squibi | Date: 09/07/2003 18:53:00 | Type : Comment |
|
| Hmm okay, thank you for the answer :) |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








