Java – use select where where Mysql to be case sensitive in MySQL

Hi, I'm 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 tried to retrieve the value of ABC, so I wrote 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

In fact, I just hope the result 'ABC' is the answer: 22

I also found the result. The code is as follows

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 get the correct output, but the result comes from Java code and is not in the query. Is it possible to retrieve the same result in the query

thank you

Solution

Use the MySQL binary operator:

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

SQL Fiddle Demo.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>