-1

how can i get exact Condition From SQL Query like below :

select * from users where name = "ned stark" order by id ;
select * from users where name = "rob stark" group by id ;

i want to get ' name = "ned stark" ' and ' name = "rob stark" '; is there way to do this ?

azro
  • 53,056
  • 7
  • 34
  • 70
Rome
  • 563
  • 1
  • 9
  • 25
  • 1
    Are you making a query to `JDBC` driver? – Saif Ahmad Nov 14 '17 at 09:44
  • 2
    Are you trying to parse SQL statements? How complex can those get? https://stackoverflow.com/questions/660609/sql-parser-library-for-java – Thilo Nov 14 '17 at 09:49
  • 1
    That is invalid standard SQL. String constants need to be enclosed in single quotes in SQL. Which DBMS are you using? –  Nov 14 '17 at 10:11
  • @a_horse_with_no_name MySQL uses backticks by default for quoting column names and then it treats double and single quotes interchangeably. You can set `sql-mode` to something that includes `ANSI_QUOTES` to change this behaviour. – Wodin Nov 14 '17 at 10:56
  • This question is very unclear. What is meant by "get exact condition"? Based on the accepted answer it seems you want to parse SQL statements yourself instead of execute them. You should specify that in future questions. – Wodin Nov 14 '17 at 10:58
  • @Wodin: but the question is not tagged with MySQL and in standard SQL the queries would be invalid (unless there are **columns** named `"ned stark"` and `"rob stark"` in the table which I doubt) –  Nov 14 '17 at 10:59
  • @a_horse_with_no_name I agree completely. I was just guessing which RDBMS these queries were intended for based on the fact that they contain double quotes when they should probably contain single quotes. That's why I upvoted your comment. – Wodin Nov 14 '17 at 11:02

1 Answers1

1

You can solve your problem using simply substring:

String aa = "select * from test where a='aa' and b='bb' order by aa";
int startIndext = aa.indexOf("where") + 5;
int endIndex = aa.indexOf("group");
if(endIndex<0) endIndex = aa.indexOf("order");
if(endIndex<0) endIndex = aa.length();
String whereCondition = aa.substring(startIndext, endIndex);
System.out.println("whereCondition: " + whereCondition);
NikNik
  • 2,191
  • 2
  • 15
  • 34