フィルターのクリア

Using Sql in matlab to get data

2 ビュー (過去 30 日間)
John Doe
John Doe 2020 年 2 月 25 日
コメント済み: John Doe 2020 年 2 月 26 日
Hello!
I hope you're doing well.
I want to get specific rows from my data base that have specific IDs.
ID = [8 , 4, 3];
ID = num2str(MissingTrips);
DD = ['SELECT * ' 'FROM Countries WHERE IDNr =',ID]; DD = fetch(conn,DD );
but matlab thinks I'm trying to horzcat when I'm trying to get the data with these IDs, I tried to do mat2str instead of num2str but nothing. How can I get my rows from my Countries table? while usind ID, all at once?
I would appreciat your help on how I can proceed.
Thank you!

採用された回答

Guillaume
Guillaume 2020 年 2 月 25 日
Two things about what you've written, a matlab one, and a sql one,
Matlab:
DD = ['SELECT * ' 'FROM Countries WHERE IDNr =']
is the same thing as
DD = ['SELECT * ', 'FROM Countries WHERE IDNr =']
It is a concatenation indeed and I recommend that you always put the , separator so the reader don't have to wonder if you made a mistake. Of course the result of that is:
DD ='SELECT * FROM Countries WHERE IDNr ='
so it's not clear why you didn't write that to start with.
SQL:
WHERE x = y
only allows you to specify one value for y, no more. So your SQL syntax is not correct. To select among a list of values, you use
WHERE x IN (val1, val2, val3, ..)
One way you could compose your statement:
ID = [8 , 4, 3];
IDstring = strjoin(compose('%d', ID), ',');
SQL = sprintf('SELECT * FROM Countries WHERE IDNr IN (%s)', IDstring);
  1 件のコメント
John Doe
John Doe 2020 年 2 月 26 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by