【问题标题】:Temporary Table and Set Variables mysql --- UPDATES临时表和设置变量 mysql --- 更新
【发布时间】:2021-08-14 20:41:33
【问题描述】:

我想创建一个临时表并利用一个变量。我查阅了文档,我相信语法是正确的,但是将它们放在一起会给我一个错误。

我的目标是创建一个日期变量,该变量始终比 now() 早 1 年,并将在我的临时表中填充一个月增量行,直到现在。任何帮助将不胜感激。

错误:

"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 'SET @start_date = DATEADD(year, -1 ,GETDATE())\n\n  WHILE @start_date < GETD' at line 4"

这是我的 SQL 语句。


        CREATE TEMPORARY TABLE helper(
          date datetime not null
          );

        SET @start_date = DATEADD(year, -1 ,GETDATE())

        WHILE @start_date < GETDATE()
        BEGIN
          INSERT INTO helper VALUES (@start_date)
          SELECT @start_date = DATEADD(MONTH, 1, @start_date)
        
        END

更新 1

使用此代码,我仍然收到语法错误

     CREATE TEMPORARY TABLE helper(
          date datetime not null
        );
          
        insert into helper (date)
          with recursive cte as (
            select curdate() - interval 12 month as date
            union all
            select date + interval 1 month
            from cte
            where date < curdate()
          )
          select date from cte;

          select * from helper;

错误

 "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 'insert into helper (date)\n          with recursive cte as (\n            select c' at line 4",

更新 2

我已将语法更改为此。我仍然收到错误,但它似乎更接近 mySql 5.7

 `
          CREATE TEMPORARY TABLE helper(
            date datetime not null
          );

          CREATE PROCEDURE dowhile()
          BEGIN
          DECLARE date datetime DEFAULT SUBDATE(curdate(), interval 12 month);
            WHILE date < curDate() DO
              INSERT INTO helper VALUES(date);
              SET date = date + interval 1 month;
            END WHILE;
          END;
          DELIMITER;

          CALL dowhile()

          SELECT date_format(log.entry_stamp, '%Y-%M') AS 'date', COUNT(log.element_id) as count FROM dm_log log
          INNER JOIN dm_element el
          ON el.element_id = log.element_id
          WHERE ? = el.dm_id
          LEFT OUTER JOIN helper h
          ON h.date = log.date
          GROUP BY date
          ORDER BY date;

          DROP TEMPORARY TABLE helper; 
             `

错误

"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 'CREATE PROCEDURE dowhile()\n          BEGIN\n          DECLARE date datetime DEFAU' at line 4",

【问题讨论】:

    标签: mysql sql database syntax backend


    【解决方案1】:

    您可以使用递归 CTE 代替 WHILE 循环:

    insert into helper (date)
        with recursive cte as (
              select curdate() - interval 12 month as date
              union all
              select date + interval 1 month
              from cte
              where date < curdate()
             )
        select date from cte;
    

    Here 是一个 dbfiddle。

    注意 MySQL 不使用getdate()。并且不支持编程块之外的while 循环。并且没有dateadd() 的三参数形式。您似乎将 MySQL 与 SQL Server 混淆了。

    在早期版本中,您可能需要明确列出值:

    insert into helper (date)
        select curdate() union all
        select curdate() - interval 1 month union all
        . . .;
    

    【讨论】:

    • 感谢您的回复。我已使用您提供的代码更新了我的问题,因为新代码仍然出现语法错误。虽然我看到你的适用于 dbfiddle。
    • @helper12345 。 . .您使用的是什么版本的 MySQL?从 8.0 版开始提供递归 CTE。
    • 如果我要使用适用于以前版本的语法,有没有可以做同样事情的语法?
    • 我错了,是 5.7。 5.7 有类似的语法吗?
    猜你喜欢
    • 2011-05-21
    • 2015-11-09
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2017-10-26
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多