Unable to place mysql query

조회 수: 1 (최근 30일)
Raj Tailor
Raj Tailor 2019년 5월 28일
편집: Raj Tailor 2019년 5월 28일
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

채택된 답변

Geoff Hayes
Geoff Hayes 2019년 5월 28일
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
Raj Tailor
Raj Tailor 2019년 5월 28일
편집: Raj Tailor 2019년 5월 28일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by