0

Sorry for such a silly question.

below is the actual query:

SELECT  DATE_FORMAT(b.maxtime, "%H:%i") as ts, b.max_value-b.min_value as 
diff FROM (SELECT max(Anzahl) as max_value ,min(Anzahl) as min_value,  
greatest(max(ts),min(ts)) as maxtime  
FROM customer where ts rlike '^2017-06-06' and Typ="B4A"
GROUP BY hour(ts)) b;   

And when I am passing it in executeQuery() method using java like below:

resultset =statement.executeQuery("SELECT DATE_FORMAT(b.maxtime,\"%H:%i\") 
as ts, b.max_value-b.min_value as diff(SELECT max(Anzahl) as max_value 
,min(Anzahl) as min_value, max(ts) as maxtime from customer where Typ = " + 
"\"" + selected_value+"\""+" "+"and ts RLIKE"+"'^"+cal_value+"'"+ "GROUP BY 
hour(ts))b");

I am getting error in SQL syntax. and Error is:

MySQL server version for the right syntax to use near '(SELECT max(Anzahl) as max_value ,min(Anzahl) as min_value, max(ts) as maxtime f' at line 1

Can someone help me out to figure out the problem.

8
  • 1
    Single quotes around B4A perhaps? Commented Jun 6, 2018 at 9:46
  • You have some missing spaces in there, which is probably what is causing the error. But you should learn about prepared statements, which would help to avoid this problem. Commented Jun 6, 2018 at 9:48
  • @DawoodibnKareem I think MySQL will accept either single or double quotes. Commented Jun 6, 2018 at 9:48
  • It shouldn't. Double quotes have a different meaning entirely. Commented Jun 6, 2018 at 9:49
  • @DawoodibnKareem they have no difference in my local mysql database(5.0),I think it might has something to do with the version Commented Jun 6, 2018 at 9:54

1 Answer 1

1

It's a typo. In the actual query there's a FROM after ...b.min_value as diff. You have left it out in the String which you are parsing to executeQuery()

...diff FROM (SELECT max(Anzahl)...

So the complete statement would be

resultset = statement.executeQuery("SELECT DATE_FORMAT(b.maxtime,\"%H:%i\") 
as ts, b.max_value-b.min_value as diff FROM (SELECT max(Anzahl) as max_value 
,min(Anzahl) as min_value, max(ts) as maxtime from customer where Typ = " + 
"\"" + selected_value+"\""+" "+"and ts RLIKE"+"'^"+cal_value+"'"+ "GROUP BY 
hour(ts))b");
Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.