Get the name of the respective PHP table in SELECT UNION

Asked

Viewed 367 times

1

Hello, everybody all right?

Does anyone know how to get the name of the respective table of the pulled value?

I have the following code:

$query= "
SELECT DISTINCT titulo, categoria, views
FROM (
  SELECT DISTINCT titulo, categoria, views FROM secao_aves UNION ALL
  SELECT DISTINCT titulo, categoria, views FROM secao_peixes
) as dados
ORDER BY views DESC
LIMIT 3
";

Then I take the data and I spin the loop:

//PEGA OS DADOS
if($stmt_mvw = $mysqli->prepare($query)){ /* INICIA DECLARAÇÃO PRINCIPAL */

    //ABRE DECLARAÇÃO
    $stmt_mvw->execute();

    //TRAZ O RESULTADO DA CONSULTA
    $stmt_mvw->bind_result($titulo, $categoria, $views);

    while($stmt_mvw->fetch()){
        echo '<a href="busca.php?table='.$NOME_DA_TABELA.'">'.$titulo.'('.$views.' visualizações)</a>';
    }
}

Let’s say my database is like this:

Table "secao_aves":

id |      titulo      | categoria | views |
1  |      Pardal      |     AA    |  250  |
2  |    Beija-Flor    |     AB    |  100  |
3  |  João-de-Barro   |     AC    |  145  |

Table "secao_pisces":

id |      titulo      | categoria | views |
1  |      Bagre       |     PX    |  180  |
2  |     Dourado      |     PY    |  165  |
3  |     Pintado      |     PZ    |  75   |

So far, quiet. For example, it would be printed this (the 3 records with more "views"):

<a href="busca.php?table=<?>">Pardal (250 visualizações)</a>
<a href="busca.php?table=<?>">Bagre (180 visualizações)</a>
<a href="busca.php?table=<?>">Dourado (165 visualizações)</a>

But actually, I’d like you to print it out like this:

<a href="busca.php?table=secao_aves">Pardal (250 visualizações)</a>
<a href="busca.php?table=secao_peixes">Bagre (180 visualizações)</a>
<a href="busca.php?table=secao_peixes">Dourado (165 visualizações)</a>

In this case, I don’t know how to pull the table name relative to the data, to create the variable $NOME_DA_TABELA :/

How to proceed? v

  • 1

    Try to put in internal consultations something like select título, categoria, views, 'nome da tabela' as tabela, so each record will have the table name as a column.

  • Oops, thank you, Anderson. I went to see the answers now. Thanks again for being willing, it really was something simple.

1 answer

2


You can create an additional 'dummy' field within SQL by identifying the table:

SELECT DISTINCT titulo, categoria, views, tabela
FROM (
  SELECT DISTINCT titulo, categoria, views, 'secao_aves' as tabela FROM secao_aves UNION ALL
  SELECT DISTINCT titulo, categoria, views, 'secao_peixes' as tabela FROM secao_peixes
) as dados
ORDER BY views DESC
LIMIT 3

So across the field tabela you get the origin of the resulting line:

titulo  categoria   views   tabela
Pardal  AA           250    secao_aves
Bagre   PX           180    secao_peixes
Dourado PY           165    secao_peixes

See the model in: http://sqlfiddle.com/#! 9/b6d0fd/3

  • Thank you very much, Rogério for being willing to show in a more direct way. It is surely more a technique pro eternal learning. A hug!

  • Thank you, reminding you to give the answer as you accept, ok?

  • Promptly. I was testing hehe but it all worked out. Until ;)

Browser other questions tagged

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