我需要使用 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/