Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not case sensitive query in mysql

Tags:

php

mysql

i need to query my database and find results :

mysql_query("select * from ".ALU_TABLE." where username like '%$q%' or name like '%$q%'");

if i have a name in my table such as Book and i enter book in search box it wont show the Book

i need to query my database as not to be case sensitive.

like image 839
Mac Taylor Avatar asked Jun 12 '10 13:06

Mac Taylor


People also ask

How do you make a case-insensitive query in MySQL?

The syntax is as follows: SELECT DISTINCT UPPER(yourColumnName) FROM yourTableName; Case 2: Using LOWER(). Here is the query to select case-insensitive distinct.

How do I stop case sensitive in MySQL?

Another way for case-insensitive matching is to use a different “collation”. The default collations used by SQL Server and MySQL do not distinguish between upper and lower case letters—they are case-insensitive by default.

Why MySQL is not case sensitive?

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

How do you make a SQL query not case sensitive?

Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.


2 Answers

You can use the LOWER() function

... WHERE LOWER(username) LIKE blabl OR LOWER(name) LIKE asdasd
like image 194
Bart van Heukelom Avatar answered Oct 06 '22 10:10

Bart van Heukelom


You need to append something like COLLATE utf8_general_ci to your query. (The _ci suffix stands for case insensitive.)

Have a look at the documentation: 9.1.7.1. Using COLLATE in SQL Statements:

With the COLLATE clause, you can override whatever the default collation is for a comparison. COLLATE may be used in various parts of SQL statements.

like image 23
aioobe Avatar answered Oct 06 '22 08:10

aioobe