mysql insert into with windows batch with str_to_date

标签 mysql batch-file

我需要使用 Windows 批处理 (.bat) INSERT 数据从一个表到另一个表。

SET thedate='02/02/13'
mysql -e "INSERT INTO dest select str_to_date($thedate,'%d/%m/%Y') from source" thebase -uroot -ppassword`"

当我将 .bat 文件拖到 cmd 窗口时,窗口显示我的代码为:

SET thedate='02/02/13'
mysql -e "INSERT INTO dest select str_to_date($thedate,'m/Y') from source" thebase -uroot -ppassword"

%d/%m/%Y 已更改为 m/Y 所以我在表 dest 中得到空值。当我直接在 phpmyadmin 中运行这个查询时,它运行良好。

我使用 xampp 18.1 Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7

最佳答案

在 Windows 批处理中,变量的语法是 %foo%,而不是 $foo。您可能将它与 bash 混淆了。因此:

  • $thedate 不是变量
  • %d/% 被认为是未设置的 d/ 变量
  • %Y 中的 % 被认为是不匹配的变量定界符并被忽略

如果复制 % 可以转义:

SET thedate='02/02/13'
echo %thedate%,'%%d/%%m/%%Y'

...打印:

'02/02/13','%d/%m/%Y'

如果可能,我建议您不要通过命令行解释器编写 SQL 字符串。很难做到正确。

关于mysql insert into with windows batch with str_to_date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14982925/

相关文章:

php - Apache 502 错误 - 服务器挂起

batch-file - 创建一个小批处理文件来管理文件

java - 为什么我需要在批处理文件中提及绝对地址?

java - 自动重启批处理文件

python - Windows Batch 使用管道启动 python 脚本

mysql - mysql MyISAM引擎中声明外键的目的

php - 需要使用 pdo 检索 sql 数据库中的单个单元格

C# 将数据库表中的所有行显示到 DataGrid

使用 group by 的 MySql 变量顺序计算无法正常工作

android - ADB shell sendevents 不工作