Search in multiple tables

Asked

Viewed 104 times

2

Hi, I’m trying to search 3 different tables, check my code:

$buscando = $pdo->query("SELECT * FROM filmes WHERE nome LIKE '%$busca%' AND status='ativo' ORDER BY nome ASC");

The tables would be: anime, movies and series. All have the same columns, ie would be WHERE nome LIKE '%$busca%' AND status='ativo' for all.

I tried to perform using this select: SELECT * FROM animes, filmes, series, but did not return anything. It could help me?

Structures of my tables:

animes: Estrutura tabela animes

filmes: Estrutura tabela filmes

series: Estrutura tabela series

If you prefer the images hosted:

animus: prntscr.com/ga9nw9 || films: prnt.sc/ga9o5w || series: prnt.sc/ga9occ

  • Use the command Union sql, which makes these junctions.

1 answer

2


Hello,

In this case you can use the clause UNION, that together two or more results in one, follows an example:

SELECT
    *
FROM
    (
        SELECT
            'animes' AS tabela,
            id,
            nome,
            url,
            imagem,
            status,
            null AS destaque
        FROM
            animes
        UNION ALL
        SELECT
            'filmes' AS tabela,
            id,
            nome,
            url,
            imagem,
            status,
            destaque
        FROM
            filmes
        UNION ALL
        SELECT
            'series' AS tabela,
            id,
            nome,
            url,
            imagem,
            status,
            null AS destaque
        FROM
            series
    ) AS t
WHERE
    t.nome LIKE '%a%'
    AND t.status='ativo'
ORDER BY
    t.nome;

In this example, it includes an extra 'column' indicating which table the record is coming from (first column). Includes all columns that are common among the 3 tables and includes the column destaque that exists in the table filmes and does not exist in the other to exemplify this behavior that, in this example, I left as default value in the other tables as null.

I created this example also in this fiddle: https://www.db-fiddle.com/f/jefqxbEkSJxArr4xQus6B2/0

Recalling that the UNION demands that all selects must have exactly the same amount of columns with the same types.

To read more about Union you can consult the official Mysql documentation: https://dev.mysql.com/doc/refman/5.7/en/union.html

  • The problem is that the table films does not have the same amount of columns as the other two.

  • Then you need to standardize in select, edit your question with the table schema I help you build the query ;)

  • animes: http://prntscr.com/ga9nw9 || filmes: https://prnt.sc/ga9o5w || series: https://prnt.sc/ga9occ

  • I also asked the question, if you prefer.

  • I edited the answer by adjusting the example to your schema :)

  • It worked perfectly, thank you very much my friend!

  • Please! we are always available!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.