我执行:SET @start_date = '2021-01-01'; SET @end_date = '2021-01-31'; SET @sql = CONCAT('SELECT * FROM your_table WHERE date_column BETWEEN ''', @start_date, ''' AND ''', @end_date, ''';');的时候mysql说我没有@start_date变量
时间: 2023-09-05 17:09:12 浏览: 124
你可以尝试将三个 SQL 语句合并成一个,或者将它们分别执行。如果你想将它们合并成一个语句,你需要在每个变量之前添加 DELIMITER //,并在最后添加 END//DELIMITER;,像这样:
```
DELIMITER //
SET @start_date = '2021-01-01' //
SET @end_date = '2021-01-31' //
SET @sql = CONCAT('SELECT * FROM your_table WHERE date_column BETWEEN ''', @start_date, ''' AND ''', @end_date, ''';') //
PREPARE stmt FROM @sql //
EXECUTE stmt //
END //
DELIMITER ;
```
这样就可以在同一个语句中定义所有变量,并执行动态 SQL 查询。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ini-development](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)