DEVPROCB DEVPROCB - 3 months ago 8
PHP Question

Create A List From a Category Section in mysql

I was wondering if mysql has a way to look at a column and only retrieve the results when it finds a unique column once. For example

if the table looks like this:

id name category
1 test Health
2 carl Health
3 bob Oscar
4 joe Technology


As you can see their are two rows that could have the same category. Is their a way to retrieve the result where the array will one only return the category once?

What I am trying to do is get all the categories in the database so I can loop through them later in the code and use them. For example if I wanted to created a menu, I would want the menu to list all the categories in the menu.

I know I can run

SELECT categories FROM dbname


but this returns duplicate rows where I only need the cateogry to return once. Is there a way to do this on the mysql side?

I assume I can just use php's
array_unique();


but I feel like this adds more overhead, is this not something MYSQL can do on the backend?

Answer

group by worked perfectly @Fred-ii- please submit this as answer so I can get that approved for you. – DEVPROCB

As requested by the OP:

You can use GROUP BY col_of_choice in order to avoid duplicates be shown in the queried results.

Reference: