I have the following data in Mysql
Category:
Salud y belleza
Tecnología
Tecnología
Cocina y hogar
Tecnología
Salud y belleza
Cocina y hogar
What I need is that when doing the query it only shows me once each word, in this way:
Salud y belleza
Tecnología
Cocina y hogar
I really don't have the slightest idea how to do it, I appreciate your comments and answers, here is my current code:
//CONSULTA DEL PRODUCTO
$stmt = $mysqli->prepare("SELECT categoria FROM productos");
$stmt->execute(); $results = $stmt->get_result(); $categorias = $results->fetch_assoc();
if ($prodOrodrigin->num_rows >= 1) {
while($rowCategoria = $results->fetch_assoc()){
echo $rowCategoria['categoria'];
}
}
As I indicate, this will show me all the category rows regardless of whether they are repeated, what I need is for it to show all the ones that are without but without being repeated.
The most logical thing is that you have a table of categories where you can obtain this data. If this is not the case and you want to get the data from the products table, then you must use the clause
DISTINCT
in the SQL query.If you want it ordered, use the
ORDER BY
Hello, you can achieve it this way:
Normal Consula:
Departure:
#Now we avoid repeated categories with "select distinc";
Departure:
#Now we achieve the same thing using GROUP BY and Min():
Departure: