classname classid ...
车辆 1 ...
车辆 2 ...
运输 3 ...
饭店 4 ...
饭店 5 ...
商店 6 ...
企业 7 ...
如何写sql才能让搜索出来的数据为以下显示(去掉classname 内重复的数据)
车辆 1 ...
运输 3 ...
饭店 4 ...
商店 6 ...
企业 7 ...
谢谢 期待高手
36 个解决方案
#1
select distinct(classname) from table
#2
select distinct classname,classid from table
#3
up
#4
select distinct classname,classid from table
#5
select count(classid) as 重复次数, min(classid) as id ,max(classname) as classname from table group by classname
#6
select distinct classname,classid from table
#7
up
#8
select distinct(classname) from table
#9
select distinct(classname) from table
#10
select distinct classname classid from table
select distinct * from table
只要+distinct 便可以消除重复
select distinct * from table
只要+distinct 便可以消除重复
#11
select distinct classname,classid from table order by classid
#12
select distinct(classname) from table
#13
如果楼主的表是只有这两个字段就可以下面的,
select distinct * from table
如果不是的话就要下面的,
select classname,classid from table where classid not in (select max(classid) as myid from tale group by classname having count(*)>1)
select distinct * from table
如果不是的话就要下面的,
select classname,classid from table where classid not in (select max(classid) as myid from tale group by classname having count(*)>1)
#14
方法一:select distinct classname from table
方法二:select unique classname from table
方法二:select unique classname from table
#15
select distinct classname from table
这句是不符合要求的。因为只有显示一个字段。
--------------------------
select distinct classname,classid from table order by classid
这句呢。也是不符合的啦。因为除非那表的字段只有一个。而且两个字段的值要一模一样。才能去掉重复。。
楼主可以试试我上面那条。
select classname,classid from table where classid not in (select max(classid) as myid from table group by classname having count(*)>1)
这句是不符合要求的。因为只有显示一个字段。
--------------------------
select distinct classname,classid from table order by classid
这句呢。也是不符合的啦。因为除非那表的字段只有一个。而且两个字段的值要一模一样。才能去掉重复。。
楼主可以试试我上面那条。
select classname,classid from table where classid not in (select max(classid) as myid from table group by classname having count(*)>1)
#16
select classname ,min(classid ) from TABLE group by classname 行不?
#17
select classname ,min(classid ) from TABLE group by classname
这个可以!
这个可以!
#18
select distinct classname,classid from table order by classid
#19
select distinct(classname),classid ...
from table
from table
#20
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
FROM test
GROUP BY name
ORDER BY id
#21
select distinct classname,classid from table
#22
select classname,classid from table1 a
where not exists(select 1 from table1 classname=a.classname and classid>a.classname)
where not exists(select 1 from table1 classname=a.classname and classid>a.classname)
#23
楼上很多人说的都有问题 因为那个classid不同 根本没有过滤掉不同的
wen01071081() 的方法可行 执行效率也高 缺点是不能查到其它字段的值
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
下面的方法可以查任意字段的值
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id )
以上查到的是同名情况下id值较小的记录 若要求id较大值只需改为 a.id>b.id
补充:上面sql只能查到有重名的记录 若要查看所有重名和非重名的记录 应修改为
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id ) or name not in (select name
from student group by name having count(name)>1)
缺点是执行效率较低
经检测可行 lz记得给分啊
wen01071081() 的方法可行 执行效率也高 缺点是不能查到其它字段的值
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
下面的方法可以查任意字段的值
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id )
以上查到的是同名情况下id值较小的记录 若要求id较大值只需改为 a.id>b.id
补充:上面sql只能查到有重名的记录 若要查看所有重名和非重名的记录 应修改为
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id ) or name not in (select name
from student group by name having count(name)>1)
缺点是执行效率较低
经检测可行 lz记得给分啊
#24
select classname ,min(classid ) from TABLE group by classname
用distinct的效率没有group by 的高。。。
用distinct的效率没有group by 的高。。。
#25
select distinct(classname) from table
#26
楼上高人
#27
select distinct(classname) from table
#28
楼主看你的意思应该是重复的记录都是错的,应该删除,教你一个高效的删除重复记录的方法:
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.classname = E.classname );
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.classname = E.classname );
#29
--上面寫的用Distinct的方法,肯定實話是錯誤的,只能返回一個字段。
--如果查詢返回的結果集只有兩個字段
Select classname, Min(classid) As classid From 表 Group By classname
--如果查詢返回的結果集有多個字段
--方法一
Select A.* From 表 A
Inner Join
(Select classname, Min(classid) As classid From 表 Group By classname) B
On A.classname = B.classname And A.classid = B.classid
--方法二
Select * From 表 A Where Not Exists(Select classid From 表 Where classname = A.classname And classid < A.classid)
--方法三
Select * From 表 A Where classid = (Select Min(classid) From 表 Where classname = A.classname)
--如果查詢返回的結果集只有兩個字段
Select classname, Min(classid) As classid From 表 Group By classname
--如果查詢返回的結果集有多個字段
--方法一
Select A.* From 表 A
Inner Join
(Select classname, Min(classid) As classid From 表 Group By classname) B
On A.classname = B.classname And A.classid = B.classid
--方法二
Select * From 表 A Where Not Exists(Select classid From 表 Where classname = A.classname And classid < A.classid)
--方法三
Select * From 表 A Where classid = (Select Min(classid) From 表 Where classname = A.classname)
#30
select classname ,min(classid ) from TABLE group by classname
#31
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
FROM test
GROUP BY name
ORDER BY id
#32
没有那么难吧
#33
select distinct(classname),min(classid) from tableName group by classname
#34
强烈支持wen01071081()
SELECT name, MIN(id) AS id
FROM table
GROUP BY name
ORDER BY id
SELECT name, MIN(id) AS id
FROM table
GROUP BY name
ORDER BY id
#35
select classname,max(classid) classid group by classname
#36
#37
delete from table_name where class_id not in(select min(class_id) from table_name)
#1
select distinct(classname) from table
#2
select distinct classname,classid from table
#3
up
#4
select distinct classname,classid from table
#5
select count(classid) as 重复次数, min(classid) as id ,max(classname) as classname from table group by classname
#6
select distinct classname,classid from table
#7
up
#8
select distinct(classname) from table
#9
select distinct(classname) from table
#10
select distinct classname classid from table
select distinct * from table
只要+distinct 便可以消除重复
select distinct * from table
只要+distinct 便可以消除重复
#11
select distinct classname,classid from table order by classid
#12
select distinct(classname) from table
#13
如果楼主的表是只有这两个字段就可以下面的,
select distinct * from table
如果不是的话就要下面的,
select classname,classid from table where classid not in (select max(classid) as myid from tale group by classname having count(*)>1)
select distinct * from table
如果不是的话就要下面的,
select classname,classid from table where classid not in (select max(classid) as myid from tale group by classname having count(*)>1)
#14
方法一:select distinct classname from table
方法二:select unique classname from table
方法二:select unique classname from table
#15
select distinct classname from table
这句是不符合要求的。因为只有显示一个字段。
--------------------------
select distinct classname,classid from table order by classid
这句呢。也是不符合的啦。因为除非那表的字段只有一个。而且两个字段的值要一模一样。才能去掉重复。。
楼主可以试试我上面那条。
select classname,classid from table where classid not in (select max(classid) as myid from table group by classname having count(*)>1)
这句是不符合要求的。因为只有显示一个字段。
--------------------------
select distinct classname,classid from table order by classid
这句呢。也是不符合的啦。因为除非那表的字段只有一个。而且两个字段的值要一模一样。才能去掉重复。。
楼主可以试试我上面那条。
select classname,classid from table where classid not in (select max(classid) as myid from table group by classname having count(*)>1)
#16
select classname ,min(classid ) from TABLE group by classname 行不?
#17
select classname ,min(classid ) from TABLE group by classname
这个可以!
这个可以!
#18
select distinct classname,classid from table order by classid
#19
select distinct(classname),classid ...
from table
from table
#20
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
FROM test
GROUP BY name
ORDER BY id
#21
select distinct classname,classid from table
#22
select classname,classid from table1 a
where not exists(select 1 from table1 classname=a.classname and classid>a.classname)
where not exists(select 1 from table1 classname=a.classname and classid>a.classname)
#23
楼上很多人说的都有问题 因为那个classid不同 根本没有过滤掉不同的
wen01071081() 的方法可行 执行效率也高 缺点是不能查到其它字段的值
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
下面的方法可以查任意字段的值
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id )
以上查到的是同名情况下id值较小的记录 若要求id较大值只需改为 a.id>b.id
补充:上面sql只能查到有重名的记录 若要查看所有重名和非重名的记录 应修改为
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id ) or name not in (select name
from student group by name having count(name)>1)
缺点是执行效率较低
经检测可行 lz记得给分啊
wen01071081() 的方法可行 执行效率也高 缺点是不能查到其它字段的值
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
下面的方法可以查任意字段的值
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id )
以上查到的是同名情况下id值较小的记录 若要求id较大值只需改为 a.id>b.id
补充:上面sql只能查到有重名的记录 若要查看所有重名和非重名的记录 应修改为
select * from student where id in(select a.id from student a,student b
where a.name=b.name and a.id<b.id ) or name not in (select name
from student group by name having count(name)>1)
缺点是执行效率较低
经检测可行 lz记得给分啊
#24
select classname ,min(classid ) from TABLE group by classname
用distinct的效率没有group by 的高。。。
用distinct的效率没有group by 的高。。。
#25
select distinct(classname) from table
#26
楼上高人
#27
select distinct(classname) from table
#28
楼主看你的意思应该是重复的记录都是错的,应该删除,教你一个高效的删除重复记录的方法:
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.classname = E.classname );
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.classname = E.classname );
#29
--上面寫的用Distinct的方法,肯定實話是錯誤的,只能返回一個字段。
--如果查詢返回的結果集只有兩個字段
Select classname, Min(classid) As classid From 表 Group By classname
--如果查詢返回的結果集有多個字段
--方法一
Select A.* From 表 A
Inner Join
(Select classname, Min(classid) As classid From 表 Group By classname) B
On A.classname = B.classname And A.classid = B.classid
--方法二
Select * From 表 A Where Not Exists(Select classid From 表 Where classname = A.classname And classid < A.classid)
--方法三
Select * From 表 A Where classid = (Select Min(classid) From 表 Where classname = A.classname)
--如果查詢返回的結果集只有兩個字段
Select classname, Min(classid) As classid From 表 Group By classname
--如果查詢返回的結果集有多個字段
--方法一
Select A.* From 表 A
Inner Join
(Select classname, Min(classid) As classid From 表 Group By classname) B
On A.classname = B.classname And A.classid = B.classid
--方法二
Select * From 表 A Where Not Exists(Select classid From 表 Where classname = A.classname And classid < A.classid)
--方法三
Select * From 表 A Where classid = (Select Min(classid) From 表 Where classname = A.classname)
#30
select classname ,min(classid ) from TABLE group by classname
#31
SELECT name, MIN(id) AS id
FROM test
GROUP BY name
ORDER BY id
FROM test
GROUP BY name
ORDER BY id
#32
没有那么难吧
#33
select distinct(classname),min(classid) from tableName group by classname
#34
强烈支持wen01071081()
SELECT name, MIN(id) AS id
FROM table
GROUP BY name
ORDER BY id
SELECT name, MIN(id) AS id
FROM table
GROUP BY name
ORDER BY id
#35
select classname,max(classid) classid group by classname
#36
#37
delete from table_name where class_id not in(select min(class_id) from table_name)