Reputation: 21371
IF (( SELECT param FROM changes WHERE type = 'uptime' AND website_id = 1 ORDER BY timestamp DESC LIMIT 1 ) != 'up' )
INSERT INTO changes ( website_id, timestamp, type, param ) VALUES ( 1, NOW(), 'uptime', 'up' )
END IF;
This appears to be an incorrect syntax. How else can I make this work in a single query?
Upvotes: 1
Views: 965
Reputation: 173
Why are you reading param record outside the query using IF instead of just adding it in the WHERE Condition? If you do it like this then you can just Insert the complete query, if the query returns something your data will be inserted, if your query returns nothing then nothing would be inserted:
INSERT INTO changes ( website_id, timestamp, type, param )
SELECT 1, NOW(), 'uptime', 'up'
FROM changes
WHERE type = 'uptime' AND website_id = 1 AND param != 'up' LIMIT 1
Upvotes: 4
Reputation: 2290
changes
seems to be a table for logging events. Given all the variations of INSERT
it is not possible to do it. You can try using STORED PROCEDURE
to achieve it. You can do something like following:
select param into @last_param from changes where website_id = 1 and type = 'uptime' limit 1;
Now @last_param will contain up
or down
. In the next statement can be conditional on if last_param
is up or down.
Upvotes: 0