visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: IF statement


By: VB guy Canada  Date: 08/07/2003 00:00:00  English  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 English  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 English  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 English  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 English  Type : Comment
Hmm okay, thank you for the answer :)

Do register to be able to answer

EContact
browser fav
page generated in 530.303960 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page