如何选择 DISTINCT * [英] How to SELECT DISTINCT *

查看:87
本文介绍了如何选择 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.

我正在使用 "SELECT * FROM Books WHERE product_status = '1'";

推荐答案

然后使用 group by 和 max.

Then use group by and max.

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

在这个例子中,column1 和 column2 是你在 Books 表中的列(比如 book_title、book_author 或类似的东西......),你可以选择 max(category) 所以只选择一个类别......

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

这是小DEMO

在此演示中,仅选择了两本书中的一本.未选择 ID 为 4 的图书,因为它的 product_status = '0'.

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

这篇关于如何选择 DISTINCT *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆