Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...