I want to execute this MySQL query:
INSERT INTO `cron-stats` (`user`) VALUES (".(int)$d['by-user'].")
Whenever such user doesn't exist yet, as in:
SELECT 1
FROM `cron-stats`
WHERE `user` = ".(int)$d['by-user']."
How can I execute this in one query?
You can try this using if else condition
you can use
ON DUPLICATE KEY UPDATE
but in order to perform the
INSERT
statement well, you need to set aUNIQUE
index on columnuser
.if the column has no
index
yet, execute the statement below,A little bit hacky, but if you use a
SELECT
derived table instead ofVALUES
you can do:SQL Fiddle demo