gpt4 book ai didi

mysql - MySQL 在 Matlab 中的一个奇怪的错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:16:54 25 4
gpt4 key购买 nike

我有两个表,分别是t1和t2,内容如下:

mysql> use test;
Database changed
mysql> select * from t1;
+----+------+
| id | val |
+----+------+
| 1 | 100 |
| 2 | 200 |
+----+------+
2 rows in set (0.00 sec)

mysql> select * from t2;
+----+-------+
| id | val |
+----+-------+
| -1 | -1000 |
| 1 | 1000 |
| 3 | 3000 |
+----+-------+
3 rows in set (0.00 sec)

在mysql命令中运行一条sql语句出现问题:

mysql> create or replace view iid as select id from t1 union select id from t2;select iid.id,t1.val,t2.val from iid left join t1 on iid.id=t1.id left join t2 on iid.id=t2.id;
Query OK, 0 rows affected (0.07 sec)

+----+------+-------+
| id | val | val |
+----+------+-------+
| 1 | 100 | 1000 |
| 2 | 200 | NULL |
| -1 | NULL | -1000 |
| 3 | NULL | 3000 |
+----+------+-------+
4 rows in set (0.00 sec)

在matlab中运行报错:

>> sqlCmd = ['create or replace view iid as select id from t1 union select id from t2;',...
'select iid.id,t1.val,t2.val from iid',...
' left join t1 on iid.id=t1.id',...
' left join t2 on iid.id=t2.id'];

conn = database('test','root','198471',...
'com.mysql.jdbc.Driver','jdbc:mysql://127.0.0.1:3306/test');
>> curs = exec(conn,sqlCmd);
>> curs.Message
ans =
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 'select iid.id,t1.val,t2.val from iid left join t1 on iid.id=t1.id left join t2 o' at line 1
>> curs = exec(conn,'select * from t1');
>> curs = fetch(curs);
>> curs.Data
ans =
1 100
2 200
>> sqlCmd

我是一个纯粹的 SQL 新手,我不知道这个错误消息。

如有任何帮助,我们将不胜感激。

最佳答案

似乎 Matlab 将您的 sqlCmd 矩阵中的每个字符串视为一个单独的 SQL 语句。当您将查询分解为字符串部分时,这将不起作用,因为并非每个部分都是有效的独立 SQL 语句。正如上面的评论所暗示的,您可能需要搜索允许这样做的设置。或者,您可以尝试将 SQL 重写为单个字符串,就像您对 mysql 所做的那样:

 sqlCmd = 'create or replace view iid as select id from t1 union select id from t2;select iid.id,t1.val,t2.val from iid left join t1 on iid.id=t1.id left join t2 on iid.id=t2.id;';
curs = exec(conn,sqlCmd);

这至少应该可以无误地运行您的查询。如果您还没有这样做,请查看 exec http://www.mathworks.co.uk/help/toolbox/database/ug/exec.html 的 mathworks 文档

关于mysql - MySQL 在 Matlab 中的一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com