简体   繁体   中英

Im getting an sql syntax error 1064 for the stored procedure below when I added the inner join and where clause

Im getting this syntax error 1064 near Inner join users u on u.id = ub.user_id WHERE u.active =0 AND IFNULL ' at line 2. Also what's the best way to debug a stored procedure??

Expected Output : delete data for not active users 
DELIMITER $$ 

CREATE PROCEDURE `logdb_user_breaks_delete`(

IN log_database VARCHAR(255) CHARACTER SET utf8mb4, 

IN retention_logdb INT(11) )

BEGIN SET @v = concat('DELETE FROM `',log_database ,'`.`user_breaks` ub
INNER JOIN users u on u.id = ub.user_id

WHERE u.active = 0 AND

IFNULL(ub.updated_at, ub.inserted_at) < (CONCAT(YEAR(NOW()), "-", MONTH(NOW()), "-

", "01") - INTERVAL ',retention_logdb,' MONTH);');

PREPARE stm FROM @v;

EXECUTE stm; 

DEALLOCATE PREPARE stm;

END$$ DELIMITER ;

When you join tables, you have to indicate which tables you are trying to delete. I'm guessing you want to delete the user_breaks rows, not the users rows. Change DELETE FROM to DELETE ub FROM .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM