简体   繁体   中英

Case Sensitive in Mysql using select where Query

Hi I am using Java front end and Mysql Backend,,,

Actually in tbl_test contains

name value
---------------
 abc   22   
 xyz   14   
 ABC   32   
 xyZ    4   
 ABc    4

In java I try to retrieve abc's value so written a code

ResultSet result=stmt.executeQuery("select value from tbl_test where name='abc'");
while(result.next())
{
     System.out.println("Answer : "+result.getInt(1));
}
result.close();

The current output is

Answer :  22
Answer :  32
Answer :  4

Actually i want the result only 'abc' ie Answer : 22

I found result also, with below code

String name="abc";
ResultSet result=stmt.executeQuery("select name, value from tbl_test where name='"+name+"'");
while(result.next())
  {
    if(name.equals(result.getString(1))
       System.out.println("Answer : "+result.getInt(2));
  }
result.close();

Now i getting correct output, but this result from java code not in query,,, Is it possible to retrieve the same result in query...

Thank you

Use the MySQL Binary operator:

select value 
from tbl_test 
where CAST(name AS BINARY) ='abc';

SQL Fiddle Demo .

You have to use case sensitive search for example, or use Binary serach like Mahmoud said

select value from tbl_test where name  COLLATE latin1_general_cs LIKE 'abc'

http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM