简体   繁体   English

SQL SELECT DISTINCT混乱

[英]SQL SELECT DISTINCT confusion

I know I can select distinct like this: 我知道我可以像这样选择不同的内容:

SELECT DISTINCT id, title, photo FROM myTable;

But when I want to SELECT id, title but with distinct title , what should I do? 但是,当我想SELECT id, titletitle不同的,该怎么办?

I mean I want to select rows and avoid duplicate titles. 我的意思是我想选择行并避免重复的标题。 Because maybe there be duplicate photos, but it's not important, I need only distinct titles . 因为也许有重复的照片,但这并不重要,所以我只需要不同的titles How then should I select photo and title fields and at the same time set title to be unique and distinct? 那我该如何选择phototitle字段,同时将title设置为唯一且与众不同?

SELECT  id, title, photo FROM myTable GROUP BY title;

使用group by的简单选择查询将在此处工作。

Depends on your data, but basically you are looking to force photo to return a single value, along with your distinct title. 取决于您的数据,但基本上您是想强迫照片返回单个值以及您的不同标题。 Something like this: 像这样:

SELECT title, MAX(photo)
FROM myTable
GROUP BY title

would do that, for example. 例如,这样做。

不同的照片Menas所有照片,没有重复的SELECT ID,来自myTable GROUP BY的照片;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM