Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Wildcard for "=" - is there one

Tags:

mysql

wildcard

So,

SELECT * FROM table WHERE col LIKE '%'

will return everything. Is there a wildcard for the query

SELECT * FROM table WHERE col = '*'

Clearly * doesn't work, I just put it there to indicate where I'd like a wildcard. The column I'm selecting from contains an integer between 1 and 12, and I want to be able to select either all records with a particular number, or all records with a wildcard.

Thanks,

like image 377
Dan Avatar asked Apr 29 '09 21:04

Dan


People also ask

What wildcards are available in MySQL?

MySQL provides two wildcard characters for constructing patterns: percentage % and underscore _ . The percentage ( % ) wildcard matches any string of zero or more characters. The underscore ( _ ) wildcard matches any single character.

Can we use wildcard in MySQL?

MySQL WildcardsA wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.


2 Answers

LIKE is basically the same as =, except LIKE lets you use wildcards.

These two queries will return the same results:

SELECT * FROM table WHERE col LIKE 'xyz';
SELECT * FROM table WHERE col='xyz';

Without a '%' in the LIKE query, it is effectively the same as '='.

If you're doing a selection on an integer column, you should consider using the IN() or BETWEEN operators. It sounds like you have two separate conditions that should be handled in your code however, rather than in the query, as your conditions dictate that you need at least two different kinds of queries.

Edit: I should clarify that LIKE and = are similar only in normal, humdrum string comparison usage. You should check the MySQL Manual for specifics on how it works, as there are situations where it's not the same (such as language sets).

like image 150
zombat Avatar answered Sep 19 '22 17:09

zombat


If you want to select everything, why are you attaching the WHERE clause at all? Just leave it off conditionally instead of putting a wildcard into it.

like image 39
Chad Birch Avatar answered Sep 20 '22 17:09

Chad Birch