选择不同值 SQL
我有这样的表格,
-------------------------------------------------------------------
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
DISTINCT
将确保没有 2 个记录的所有列都匹配,因此此工作正常。如果您想返回唯一的标题,您需要决定返回什么图像和名称。
您可以使用带有聚合函数的 group by 来执行此操作。例如:
但这取决于你想要什么结果......
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:
But it depends on what results you are after...
您将需要指定获胜者...换句话说,如果有重复的标题但其他列中的数据不同,您需要选择一个...
例如您可以尝试这个。
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.
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 isGROUP BY
:SELECT title,image,name,id FROM some_table GROUP BY title