我想我以绝对错误的方式使用这些MySQL控制结构(见下文)。问题可能是在MySQL查询中,但我不确定...
MySQL Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'IF @records > 1000 THEN SELECT @actRecords:=COUNT(id) FROM
sys_proc_err WHERE ' at line 1
#include
// other includes, declarations for mysql... etc.
int main() {
mysql_init(&mysql);
mysql_real_connect(&mysql,DB_SERVER,DB_USER,DB_PASSWORD,DB_DB_IN,0,0,CLIENT_MULTI_STATEMENTS);
// Now: DELETE ERROR RECORDS older than 3 days if there is more than 1 000 records, leave 1 000 newest records
mysql_query(connection, "SELECT @records:=COUNT(`id`) FROM `sys_proc_err`;
IF @records > 1000 THEN SELECT @actRecords:=COUNT(`id`) FROM `sys_proc_err` WHERE time > (NOW()-259200);
END IF;
IF @actRecords > 1000 THEN DELETE FROM `sys_proc_err` WHERE `time` < (NOW()-259200);
ELSE DELETE FROM `sys_proc_err` ORDER BY `id` ASC LIMIT @actRecords-1,@records-@actRecords;
END IF;");
return 0;
}SELECT @records:=COUNT(id) FROM sys_proc_err的结果是1267。
SELECT @actRecords:=COUNT(id) FROM sys_proc_err WHERE time > (NOW()-259200)的结果是6。