Brackets within a SQL query
Show older comments
Hi everybody,
I'm using the fetch function (results = fetch(conn,sqlquery)) to get data from a database into MATLAB. In the database there are many tables and I want to create a query that enables me to get data from different tables (in this case the first 5 entries of every table). Doing so I tried those queries to get data from a single table:
sqlquery1 = ['SELECT a, b FROM table1 LIMIT 5];
sqlquery2 = ['SELECT a, b FROM table2 LIMIT 5];
Due to the fact that I have to get data from 30 and more tables I don't want to send a single query each time. So I tried:
sqlquery = ['(SELECT a, b FROM table1 LIMIT 5) UNION (SELECT a, b FROM table2 LIMIT 5)'];
but it wouldn't work.
Due to this I also tried:
sqlquery1 = ['(SELECT a, b FROM table1 LIMIT 5)'];
which wouldn't work either.
Do you have any ideas why MATLAB doesn't accept the brackets?
Is there another way to solve the problem?
Cheers,
Christoph
1 Comment
Guillaume
on 20 Jul 2016
I assume you're talking about the curved brackets () around the SELECT query.
I was a bit confused at first because of the square brackets [] in your post that serve absolutely no purpose.
string = ['xxxx']
is exactly the same as
string = 'xxxx'
Answers (1)
the cyclist
on 20 Jul 2016
0 votes
I successfully returned results from a query of this format (against a Postgres 9.5 database). I have two suggestions:
- Issue that same query directly against the database (not via MATLAB) to see if you get any kind of error.
- Post the full error message you are getting, so we can take a more detailed look
Categories
Find more on Database Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!