Retrieve data from a table in a Database with incomplete conditions

0

In a table of a Database I have the following:

id_video | nom_video 
---------+-----------------
1        | Base de datos 
2        | Programación

With the following instruction I recover what is equal to "Database", but as I recover what contains part of the name for example if I put only "base" or "data".

select * 
  from unidadAprendizaje 
  where nom_video="Base de datos";
    
asked by user110619 15.12.2018 в 08:16
source

1 answer

1

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 ( _ ).

  • The wildcard percentage ( % ) allows you to match any string of zero or more characters.

  • The underscore wildcard ( _ ) allows you to match any individual character.

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

answered by 15.12.2018 в 10:39