MATLAB Answers

Unable to place mysql query

9 views (last 30 days)
Raj Tailor
Raj Tailor on 28 May 2019
Edited: Raj Tailor on 28 May 2019
Hello Everyone,
I am trying to make following query to the mysql database :
This ishow query will look and run in mysql
SELECT * FROM table_name WHERE start_time_local BETWEEN '2019-05-25T00:00:00' AND '2019-05-27T24:00:00' AND latitude_eb BETWEEN '52.5012' AND '52.5052' ;
I have written it in following way in matlab
TIMESTART='2019-05-25T00:00:00';
TIMEEND='2019-05-27T24:00:00';
lat_st='52.5012';
lat_end='52.5052';
%Read Mysql database for specific month using query
qr1='SELECT subject,start_time_local,end_time_local,city,street_eb,latitude_eb,longitude_eb FROM ';
qr2=" ";
qr=strcat(qr1,qr2,table_name,qr2);
qr=char(qr);
sqlquery = [qr ...
'WHERE start_time_local BETWEEN' '''' TIMESTART '''' 'AND' '''' TIMEEND ''''...
'AND' 'latitude_eb BETWEEN' '''' lat_st '''' 'AND' '''' lat_end ''''];
read_mysql_databse = select(conn,sqlquery); %dot work with select
With one clause it is working fine but when I use second AND and BETWEEN statement, it throws following error.
Error using database.odbc.connection/select (line 213)
ODBC Driver Error: [MySQL][ODBC 5.3(a) Driver][mysqld-8.0.13]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 'ANDlatitude_eb BETWEEN'52.5012'AND'52.5052'' at line 1
Would appreciate the earliest help and recomendations.
Regards,
Raj

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 May 2019
Raj - it looks like you are missing some spaces between the word AND and the column
'ANDlatitude_eb BETWEEN'52.5012'AND'52.5052''
You will need to add some spaces like
'WHERE start_time_local BETWEEN ' '''' TIMESTART '''' ' AND ' '''' TIMEEND ''''...
' AND ' 'latitude_eb BETWEEN ' '''' lat_st '''' ' AND ' '''' lat_end ''''];
Try the above and see what happens!
  1 Comment
Raj Tailor
Raj Tailor on 28 May 2019
Hello Geoff,
I tried following way and it worked, thank you for the help and quick response.
Much appreciated :)
%Read Mysql database for specific month using query
qr1='SELECT subject,start_time_local,end_time_local,city,street_eb,latitude_eb,longitude_eb FROM ';
qr2=" ";
qr=strcat(qr1,qr2,table_name,qr2);
qr=char(qr);
sqlquery = [qr ...
'WHERE start_time_local BETWEEN' '''' TIMESTART '''' 'AND' '''' TIMEEND ''''...
'AND latitude_eb BETWEEN' '''' lat_st '''' 'AND' '''' lat_end ''''];
read_mysql_databse = select(conn,sqlquery); %dot work with select

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by