What is undescore for "_" in MYSQL LIKE?

6

In a previous question regarding LIKE in MYSQL , Should I avoid injecting"% "into a query where I used" LIKE "? , a question arose of _ (undescore) can be used in the LIKE operator.

I've used the % a lot, but this wilcard _ I did not know. So that it serves in LIKE of Mysql ?

    
asked by anonymous 17.09.2015 / 19:14

2 answers

11

The underline ( _ ) is to compare any character only once, while the percentage ( % ) means any character in any quantity.

( _ ) is equivalent to the meta character ( . ) of regular expressions and ( % ) to ( .* )

MySQL-like

    
17.09.2015 / 19:20
2

The underscores _ is to find occurrences in certain position of the string.

Let's suppose I want to search for all strings that start with my .

LIKE 'my%'

They will return me records like: mySQL , Myan , Mylan and etc.

Let's start from the example I wanted to search for my but from the third position of the string.

LIKE '___my%'

They will return me records as hjjmy , yuimy , and so on.

It also has other pattern matching for like such as \_ and even ESCAPE .

Edit

Also to mark event locations.

As in the example:

SELECT 'David!' LIKE '_____';

It will have output 0 because I searched for strings that contains 5 occurrences, since David! contains 6 occurrences.

If I use:

SELECT 'David!' LIKE '______';

Will have output 1

Already an expression:

SELECT 'David32' LIKE '_____';

It will confront if

17.09.2015 / 19:28