I use the following excerpt for one queried to the database
$cs1 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5")->fetchAll();
I would then like to explore this query further in order to use as little memory as possible.
This is the following on the home page of the site I'm using two different queries
$cs1 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5")->fetchAll();
$cs2 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY VOTOS DESC LIMIT 5")->fetchAll();
As you can see, the only thing that changes between queries is the column quoted in ORDER BY
, where in one the cited column of ORDER BY
is CLIQUES
and in other VOTOS
.
In my view this is a waste of resources, since both queries are displayed on the same page. Is there any way to make a single query yet determine different columns in ORDER BY
? Something like this:
<?php
$colunas = ID, VOTOS, CLIQUES, CATEGORIA;
$csu = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY $colunas DESC LIMIT 5")->fetchAll();
?>
Alike type, every time you reach the limit of 5, skip to next term quoted in the variable $colunas