Get Full MySQL Query String on Insert or Update

后端 未结 8 1047
旧巷少年郎
旧巷少年郎 2020-12-13 10:16

Need help with MySQL as it\'s not really my forte. So any help is appreciated.

I have issues on my site where UPDATE or INSERT were done wi

8条回答
  •  半阙折子戏
    2020-12-13 11:01

    You can get the current SQL query as a string with the following statement:

    SELECT info FROM INFORMATION_SCHEMA.PROCESSLIST WHERE id = CONNECTION_ID()
    

    So what you have to do is to create a TRIGGER which runs on insert and/or update operations on your table which should (i) get the current sql statement and (ii) insert it into another table, like so:

    DELIMITER |
    
    CREATE TRIGGER log_queries_insert BEFORE INSERT ON `your_table`
    FOR EACH ROW
    BEGIN
        DECLARE original_query VARCHAR(1024);
        SET original_query = (SELECT info FROM INFORMATION_SCHEMA.PROCESSLIST WHERE id = CONNECTION_ID());
        INSERT INTO `app_sql_debug_log`(`query`) VALUES (original_query);
    END;
    |
    DELIMITER ;
    

    You will have to create two triggers - one for updates and one for inserts. The trigger inserts the new query as a string in the app_sql_debug_log table in the query column.

提交回复
热议问题