Is it possible to do a kind of WHERE *?

1

I seek to make a query that makes a complete selection of the tabla that I select where any of all the existing fields in the db match the search value, this implies that the data that exists in the base of data with a value equal to the one entered, I mean:

I have a search field in which to enter a word I want to be searched in the db.

$sql="SELECT * FROM tbl_noticias_blog WHERE unodetodoslosdatos='$busqueda'";

At the moment I have it in this way the code:

$sql="SELECT * FROM tbl_noticias_blog WHERE fecha='$busqueda' OR titulo='$busqueda' OR dcorta='$busqueda' OR dlarga='$busqueda'"

This way the person will enter a search value and it should be compared with all the tables to see if it exists.

    
asked by felipe andrade 05.09.2017 в 16:37
source

3 answers

1

What you need to do is a

SELECT <CAMPOS> 
FROM <TABLA>
WHERE MATCH (<CAMPO_1>, <CAMPO_2>) 
AGAINST (<VALOR DE LA BÚSQUEDA INGRESADA>)

Then, in your case, it would be something like this:

$sql = "SELECT * 
        FROM tbl_noticias_blog 
        WHERE MATCH ('nombre_de_tus_campos') 
        AGAINST ('" . $busqueda . "')";

You can use Boolean Full-Text Searches to use operators within the search and specify if you want it to be a type search AND or NOT in the following way, (if no operator is specified, the OR will be used by default):

+ = AND

- = NOT

[ningún operador] = OR

Then you can also try with a query like this:

SELECT * 
FROM tbl_noticias_blog 
WHERE MATCH ('titulo', 'descripcion') 
AGAINST ('+Titulo' IN BOOLEAN MODE)

The one that can be translated as you are looking for rows where the value of 'Titulo' is found in both the titulo field and the descripcion field

    
answered by 05.09.2017 в 16:54
1

To find a value in different columns of a table you need to create a FULLTEXT index that indicates the columns that will intervene in the search, for example:

    FULLTEXT(contenido1, contenido2)

The query would then be like this:

SELECT * FROM buscar_20170906 
    WHERE MATCH(contenido1, contenido2) 
    AGAINST ('lo que busco');
  • In MATCH() the columns involved, those with index FULLTEXT
  • In AGAINST() the value to search.

Código: Ver Demo

CREATE TABLE IF NOT EXISTS buscar_20170906 (
    publicacion_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    contenido1 TEXT,
    contenido2 TEXT,
    FULLTEXT(contenido1, contenido2)
);

INSERT INTO buscar_20170906 (contenido1, contenido2)
    VALUES 
        ('Lorem ipsum','lo que busco'),
        ('Otro Lorem ipsum de Santiago',''),
        ('dolor sit amet', 'lo que busco'),
        ('Otro dolor sit amet','')
;

SELECT * FROM buscar_20170906 
    WHERE MATCH(contenido1, contenido2) 
    AGAINST ('lo que busco');

Resultado:

publicacion_id       contenido1      contenido2
1                    Lorem ipsum     lo que busco
3                    dolor sit amet  lo que busco

Notes:

  • There are several types of search FULLTEXT , it is important that you read about it the documentation about fulltext search
  • If the tables are in InnoDB and your version of MySQL is less than 5.6, it is not possible to use FULLTEXT . In that sense you must update MySQL or create a table of the type MyISAM.
answered by 05.09.2017 в 18:55
0

You could do it with some artifice, it occurs to me to join the fields so as not to compare one by one:

select * from tbl_noticias_blog where (campo1+campo2+campo3+campon) like '%$busqueda%'

select * from tbl_noticias_blog where CONCAT(campo1,campo2,campo3,campon) like '%$busqueda%'

select * from tbl_noticias_blog where (campo1||campo2||campo3||campon) like '%$busqueda%'

The way of joining varies according to the database engine.

    
answered by 05.09.2017 в 16:50