Skip to content
Advertisement

How to SELECT DISTINCT *

Is this a possible query?

Currently, every product from my database is being listed on the home page of my website and a lot of them are duplicates since a product can belong to multiple categories.

I am using "SELECT * FROM Books WHERE product_status = '1'";

Advertisement

Answer

Then use group by and max.

select column1, column2, max(category)
from Books 
WHERE product_status = '1'
group by column1, column2;

In this example column1 and column2 are your columns from table Boooks (like book_title, book_author or something similar…) and you can select max(category) so only one category is selected….

Here is the small DEMO

In this demo only one of two same books is selected. Book with ID 4 is not selected because it has product_status = ‘0’.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement