2
I use the following passage for a query to the database
$cs1 = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY CLIQUES DESC LIMIT 5")->fetchAll();
I would like to explore this consultation further in order to use as little memory as possible.
Here’s the thing, on home page 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 cited in ORDER BY
, where in a cited column of ORDER BY
is CLIQUES
and the other VOTOS
.
In my view this is a waste of resources, since both queries are displayed on the same page. There is then some way to make a single query to determine different columns in the ORDER BY
? Something like that :
<?php
$colunas = ID, VOTOS, CLIQUES, CATEGORIA;
$csu = $pdo->query("SELECT TITULO, URL, IMAGEM , TEMPO FROM post ORDER BY $colunas DESC LIMIT 5")->fetchAll();
?>
Ai type, each time you reach the limit of 5, skip to the next term cited in the variable $colunas
Why not bring the data without defined order and then sort in
PHP
?– Omni
How so @Omni
– ivan veloso