简体   繁体   中英

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?

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 . How then should I select photo and title fields and at the same time set title to be unique and distinct?

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的照片;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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