Code: Select all
IF (SELECT * FROM t1 WHERE page='home.htm')
UPDATE t1 SET hits=(hits+1) WHERE page='home.htm'
ELSE
INSERT INTO t1 (page, hits) VALUES ('home.htm', 1)
END IFModerator: General Moderators
Code: Select all
IF (SELECT * FROM t1 WHERE page='home.htm')
UPDATE t1 SET hits=(hits+1) WHERE page='home.htm'
ELSE
INSERT INTO t1 (page, hits) VALUES ('home.htm', 1)
END IFCode: Select all
$query = "select stuff from place";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ( $num_rows > 0 )
run update query;
else
run insertion query;Code: Select all
IF EXISTS (SELECT * FROM t1 WHERE page='home.htm') THEN
UPDATE t1 SET hits=hits+1 WHERE page='home.htm';
ELSE
INSERT INTO t1 (page, hits) VALUES ('home.htm', 1);
END IF;Thanks again for all of your replies. I'm marking the thread 'SOLVED'.If you specify ON DUPLICATE KEY UPDATE (added in MySQL 4.1.0), and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;