选择不同值 SQL

发布于 2024-12-13 09:31:01 字数 482 浏览 0 评论 0原文

我有这样的表格,

-------------------------------------------------------------------
id | title | image  | name |
-------------------------------------------------------------------
1  | xyzab | so.jpg | googl |
2  | acbde | am.jpg | artic |
3  | xyzab | pp.jpg | other |

我想选择唯一或不同的标题及其图像和名称。 不想重复这些值。我使用这个代码

SELECT DISTINCT title,image,name,id FROM `some_table`

,但这不能正常工作

注意:OP正在与MySQL一起使用

I have table like this

-------------------------------------------------------------------
id | title | image  | name |
-------------------------------------------------------------------
1  | xyzab | so.jpg | googl |
2  | acbde | am.jpg | artic |
3  | xyzab | pp.jpg | other |

i want to select unique or distinct title with it's image and name also.
DO not want to repeat the values. I use this this code

SELECT DISTINCT title,image,name,id FROM `some_table`

but this is not working fine

NOTE: The OP is working with MySQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卷耳 2024-12-20 09:31:01

使用 DISTINCT 将确保没有 2 个记录的所有列都匹配,因此此工作正常。

如果您想返回唯一的标题,您需要决定返回什么图像和名称。

您可以使用带有聚合函数的 group by 来执行此操作。例如:

SELECT title, MIN(image), MIN(name), MIN(id)
FROM `some_table`
GROUP BY title

但这取决于你想要什么结果......

Using DISTINCT will ensure no 2 records have all columns matching, so this is working correctly.

If you want to return unique titles, you need to decide what image and name would be returned.

You could use a group by with an aggregate function to do this. For example:

SELECT title, MIN(image), MIN(name), MIN(id)
FROM `some_table`
GROUP BY title

But it depends on what results you are after...

琉璃繁缕 2024-12-20 09:31:01

您将需要指定获胜者...换句话说,如果有重复的标题但其他列中的数据不同,您需要选择一个...

例如您可以尝试这个。

select * from 'some_table' where id in (select min(id) from 'some_table' group by title)

You will need to specify the WINNER... in other words if there is a duplicate title but differening data in other columns you need to pick one...

For example you could try this.

select * from 'some_table' where id in (select min(id) from 'some_table' group by title)
花开雨落又逢春i 2024-12-20 09:31:01

DISTINCT 不适用于关键字后的一个字段,而是适用于 select 语句中的字段。您要查找的是GROUP BY

SELECT title,image,name,id FROM some_table GROUP BY title

DISTINCT is not applied to the one field after the keyword, but for fields in your select statement. What you're looking for is GROUP BY:

SELECT title,image,name,id FROM some_table GROUP BY title

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文