使用 SELECT INTO OUTFILE 时出现语法错误

发布于 2024-10-04 13:02:41 字数 598 浏览 5 评论 0原文

以下查询时

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;

当我运行直接来自 MySQL 文档的 ,出现语法错误。我没有 test_table 表,并且我知道该语句实际上不起作用,但它似乎不应该给出语法错误。如果我直接从 MySQL 文档中得到语法错误,可能会发生什么情况?

这是我正在看的文档(5.1): http://dev.mysql.com/doc/refman/5.1/en /select.html

这是我的 MySQL 版本:

mysql  Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1

When I run the following query

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;

which is straight from the MySQL documentation, I get a syntax error. I don't have a test_table table and I understand that this statement won't actually work, but it doesn't seem like it should give a syntax error. If I'm getting a syntax error on something straight from the MySQL documentation, what could be going on?

This is the doc I'm looking at (5.1):
http://dev.mysql.com/doc/refman/5.1/en/select.html

This is my MySQL version:

mysql  Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-10-11 13:02:41

这段代码对我有用。

起初,我遇到了 Unknown table 'test_table' in my_db 错误。

创建表 CREATE TABLE test_table (a VARCHAR(255), b VARCHAR(255)) 后,它就正常工作了。没有语法错误。

编辑:

从官方 mysql 命令行客户端运行查询可以避免 GUI 客户端中潜在的错误,并提供更准确的错误消息。

This code works for me.

At first I had an Unknown table 'test_table' in my_db error.

After creating the table CREATE TABLE test_table (a VARCHAR(255), b VARCHAR(255)) it just worked. No syntax error.

Edit:

Running the query from the official mysql command line client can avoid potential bugs in your GUI client and give more accurate error messages.

我不在是我 2024-10-11 13:02:41

也许您必须根据您使用的语言转义某些字符。

示例:

$sql = "SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'"
    . " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"
    . " LINES TERMINATED BY '\\n'"
    . " FROM test_table;";

Maybe you have to escape some of the characters depending on the language you are using.

php example:

$sql = "SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'"
    . " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"
    . " LINES TERMINATED BY '\\n'"
    . " FROM test_table;";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文