平時我都會先select count(1) 看看有沒有已存表格, 再分別insert 或 update, 今天才發現原來可以一句ko:
ON DUPLICATE KEY UPDATE
CREATE TABLE ipstat(ip int UNSIGNED NOT NULL PRIMARY KEY, hits int UNSIGNED NOT NULL, last_hit timestamp);
INSERT INTO ipstat VALUES(inet_aton('192.168.0.1'),1,now()) ON duplicate KEY UPDATE hits=hits+1;