作业帮 > 综合 > 作业

distinct 两个字段

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/04/30 15:38:17
distinct 两个字段
distinct 两个字段
后面跟多个字段的话,就是要多个字段同时重复才能过滤
其实道理很简单,举个例子,如果数据是
id name
1 aaa
2 aaa
select distinct id,name from tab
这个语句的返回结果必然是两条,因为如果是1条的话,id 有两个值,就没法显示,随机取数这种事情如果没有指令,数据库自己是没有这个功能的.
解决方法:
比如name相同的记录,我要id最小的
select * from tab a where not exists (select 1 from tab where name = a.name and id < a.id)
搞定!
求采纳为满意回答.