Matlab与MySQL中的奇怪错误

时间:2012-08-25 08:11:57

标签: mysql database matlab

我有两个名为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的纯新手,我不知道这个错误信息。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

似乎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文档