Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'";

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 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'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...