Question

what is the difference between the below like commands in mysql

SELECT * FROM dbname WHERE filedname LIKE '12%';

and

SELECT * FROM dbname WHERE filedname LIKE '%12';
Was it helpful?

Solution

The percent is a wildcard that will match anything. So '12%' will match '12abc', but it won't match 'abc12'. While '%12' will do the opposite.

OTHER TIPS

in

SELECT * FROM dbname WHERE filedname LIKE '12%';

you'r searcing a filedname value that start with 12 like : "12whatever" and in

SELECT * FROM dbname WHERE filedname LIKE '%12';

you'r searcing a filedname value that end with 12 like : "what ever 12"

The % symbol means that anything can be in that side and it will match.

For example with '12%' would match '12345' or '12 dogs', or with '%12' would match '2012' or 'I am 12'.

When LIKE is used along with % sign then it will work like a meta character search. The percent sign wildcard can be used to represent any number of characters in a value match.

for example to list all the rows where the product name ends in the words 'Computer Case':

SELECT * FROM product WHERE prod_name LIKE '% Computer Case';
+-----------+--------------------+-------------------+
| prod_code | prod_name          | prod_desc         |
+-----------+--------------------+-------------------+
|        11 | Grey Computer Case | ATX PC CASE       |
|        12 | Gray Computer Case | ATX PC CASE (USA) |
+-----------+--------------------+-------------------+

Alternatively, the wildcard could be placed at the end of a value, for example to retrieve all the items in stock manufactured by the fictitious WildTech Corporation:

SELECT * FROM product WHERE prod_name LIKE 'WildTech%';
+-----------+--------------------------+------------------------+
| prod_code | prod_name                | prod_desc              |
+-----------+--------------------------+------------------------+
|         3 | WildTech 250Gb 1700      | SATA Disk Drive        |
|        13 | WildTech Mouse (Optical) | Wireless Optical Mouse |
+-----------+--------------------------+------------------------+
2 rows in set (0.00 sec)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top