Welcome to Stackoverflow.
To do what you want there is the LIKE
operator, which is commonly used to select columns with specific patterns. Using the operator LIKE
in the correct way is essential to increase the performance of the query. It is often used in the WHERE
clause of the SELECT
statement.
MySQL provides two wildcard characters to use with the operator LIKE
, percentage ( %
) and underscore ( _
).
Let's see some examples:
Suppose you want to search for a nom_video
that contains the word base only , you can use the% wild_cound% in this way:
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE '%base%';
Or you want to find the words base OR data :
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE '%base%' OR nom_video LIKE '%datos%';
Or you want to find the words base and data :
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE '%base%' AND nom_video LIKE '%datos%';
In these cases, use the notation %
, that is, the pattern enclosed between the wildcard %patron%
, this can be understood as contains [pattern] .
%
you have more uses, depending on the position of the wild card, in the previous examples we saw its use, surrounding the word with the wildcard LIKE
.
But if we use this notation: %
, we would be looking for values that begin for [patron] :
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE 'base%';
And if we use this notation: patron%
, we would be looking for values that end for [patron] :
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE '%base';
And finally, just to show how the other wild card works. If you want to find the values that start with %patron
, have any character and then follow a b
, you use the wildcard: s
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE 'b_s';
In the case of this wildcard, every _
represents a character, you can write queries like this for example:
SELECT *
FROM unidadAprendizaje
WHERE nom_video LIKE 'b__e';
There you would find values like: _
, base
, bate
...
There are many more uses and combinations of bote
, you can see them in the documentation.
Links