如何实现查询不出现重复

ppotddy 2008-03-24 06:58:15
比如我有一个表

main_table

id name
1 aa
2 bb
3 cc
4 aa
5 dd
6 ee

希望查询出来的表是
name
aa
bb
cc
dd
ee

让重复的数据只出现一次

谢谢大家,能帮忙看看吗?
...全文
142 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2008-03-24
  • 打赏
  • 举报
回复
main_table

id name
1 aa
2 bb
3 cc
4 aa
5 dd
6 ee

希望查询出来的表是
name
aa
bb
cc
dd
ee
select distinct name from tb order by name



如果是

main_table

id name other
1 aa 00
2 bb 00
3 cc 00
4 aa 00
5 dd 00
6 ee 00

查询出来的表是

name other
aa 00
bb 00
cc 00
dd 00
ee 00
select distinct name ,other from tb order by name


如果是

main_table

id name other
1 aa 00
2 bb 00
3 cc 00
4 aa 00
5 dd 00
6 ee 00

查询出来的表是

id name other
1 aa 00
2 bb 00
3 cc 00
5 dd 00
6 ee 00
select t.* from main_table t where id = (select min(id) from main_table where name = name) order by t.name


如果是

main_table

id name other
1 aa 00
2 bb 00
3 cc 00
4 aa 00
5 dd 00
6 ee 00

查询出来的表是

id name other
4 aa 00
2 bb 00
3 cc 00
5 dd 00
6 ee 00
select t.* from main_table t where id = (select max(id) from main_table where name = name) order by t.name


viva369 2008-03-24
  • 打赏
  • 举报
回复
来晚了
ppotddy 2008-03-24
  • 打赏
  • 举报
回复
谢谢大家,我明白了

distinct 应该出去相同的行吧?
jinjazz 2008-03-24
  • 打赏
  • 举报
回复
select distinct name,other from main_table就可以了
bqb 2008-03-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ppotddy 的回复:]
谢谢jinjazz ,记录不需要一样多,只是要求name有重复字段的消息就不显示了
引用 8 楼 hery2002 的回复:

SQL code
SELECT NAME ,OTHER FROM # GROUP BY NAME,OTHER ORDER BY NAME , OTHER



这里还有些不清楚,对于上面这个SQL语句他是怎么分组的,
因为other的数据都是重复的,我有点糊涂了
[/Quote]

先按 NAME分组,再按OTHER分组, 楼主可以先看一下联机丛书 GROUP BY 的用法!
ppotddy 2008-03-24
  • 打赏
  • 举报
回复
谢谢jinjazz ,记录不需要一样多,只是要求name有重复字段的消息就不显示了
[Quote=引用 8 楼 hery2002 的回复:]

SQL code
SELECT NAME ,OTHER FROM # GROUP BY NAME,OTHER ORDER BY NAME , OTHER

[/Quote]

这里还有些不清楚,对于上面这个SQL语句他是怎么分组的,
因为other的数据都是重复的,我有点糊涂了

bqb 2008-03-24
  • 打赏
  • 举报
回复
楼主看一下,也许对你有用!


create table tb(id int, name varchar(10), other varchar(10))
insert into tb
select 1, 'aa','00' union all
select 2, 'bb','00' union all
select 3, 'cc','00' union all
select 4, 'aa','00' union all
select 5, 'dd','00' union all
select 6, 'ee','00'

select * from tb a where not exists (select 1 from tb where name=a.name and id<a.id)

/*
id name other
-------------------------
1 aa 00
2 bb 00
3 cc 00
5 dd 00
6 ee 00
*/

drop table tb
jinjazz 2008-03-24
  • 打赏
  • 举报
回复
你仔细想想看你要的结果表和原始表记录是不是一样多...
bqb 2008-03-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ppotddy 的回复:]
引用 6 楼 jinjazz 的回复:
select name,min(other)as other from table1 group by name


这里不使用函数可以吗?
[/Quote]

不行!
ppotddy 2008-03-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jinjazz 的回复:]
select name,min(other)as other from table1 group by name
[/Quote]

这里不使用函数可以吗?
hery2002 2008-03-24
  • 打赏
  • 举报
回复
如果Other栏位的值一致的话,即如name ='aa' ,对应的Other都为同一个值,

SELECT NAME ,OTHER FROM # GROUP BY NAME,OTHER ORDER BY NAME , OTHER

如果Other栏位的值不一致,那么就需要使用相应的group函数,取Other的Max,Min等.

SELECT NAME ,Max(OTHER) FROM # GROUP BY NAMEORDER BY NAME

ppotddy 2008-03-24
  • 打赏
  • 举报
回复
谢谢,如果不使用聚合函数可以吗?
jinjazz 2008-03-24
  • 打赏
  • 举报
回复
select name,min(other)as other from table1 group by name
ppotddy 2008-03-24
  • 打赏
  • 举报
回复
谢谢,如果是

main_table

id name other
1 aa 00
2 bb 00
3 cc 00
4 aa 00
5 dd 00
6 ee 00

查询出来的表是

name other
aa 00
bb 00
cc 00
dd 00
ee 00

只对name进行不能重复的判断呢?
bqb 2008-03-24
  • 打赏
  • 举报
回复
select distinct name from table1

select name from table1 group by name
jinjazz 2008-03-24
  • 打赏
  • 举报
回复
select distinct name from main_table order by name
select name from main_table group by name order by name
internetroot 2008-03-24
  • 打赏
  • 举报
回复
select distinct name from main_table 
internetroot 2008-03-24
  • 打赏
  • 举报
回复
select distinct name from table1

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧