首页 新闻 会员 周边

distinct 问题。。。。求大神

0
悬赏园豆:5 [已解决问题] 解决于 2013-03-20 16:49
我就想根据这个 CarBrand 来去掉重复数据 不知道可不可以》??帮忙看看代码
View Code
select distinct (t1.CarBrand), t1.id, 
t1.CarName,t1.CarColor,t1.CarType,t1.CarNotes,
t1.CarState,t1.WingNum,t1.NumL,t1.SeatNum,t1.DangWei,
t1.CompanyId,t1.ShopsId,t1.Grade,t1.CarPrice,t1.FestivalPrice,
t1.WeekendPrice,t1.PrePrice,t1.Range,t1.ServicePrice,t1.OutRangePrice,
t1.OutTimePrice,t1.ProtectPrice,t1.IsOil,t1.IsDriver,t1.IsLuqiao,
t1.ServiceTime,t2.ShopName,t2.ShopAddress,t2.AreaCode,dbo.Fun_CarACityById(t2.CityCode) as CityCode,
dbo.FunCarBrandById(t1.CarBrand) as CarBrandName,t1.TJJG,t1.BegTime,t1.TJ,t1.Image,t1.EndTime
from CarInfo t1 left join CompanyShops t2 on (t1.ShopsId=t2.Id)  order by t1.CarBrand

CarBrand  是数字。。。

问题补充:

.....................................................

次时代的主页 次时代 | 初学一级 | 园豆:5
提问于:2012-12-24 11:38
< >
分享
最佳答案
0

select * from
(
 SELECT A.* FROM
 (
  select distinct (t1.CarBrand), t1.id,
  t1.CarName,t1.CarColor,t1.CarType,t1.CarNotes,
  t1.CarState,t1.WingNum,t1.NumL,t1.SeatNum,t1.DangWei,
  t1.CompanyId,t1.ShopsId,t1.Grade,t1.CarPrice,t1.FestivalPrice,
  t1.WeekendPrice,t1.PrePrice,t1.Range,t1.ServicePrice,t1.OutRangePrice,
  t1.OutTimePrice,t1.ProtectPrice,t1.IsOil,t1.IsDriver,t1.IsLuqiao,
  t1.ServiceTime,t2.ShopName,t2.ShopAddress,t2.AreaCode,dbo.Fun_CarACityById(t2.CityCode) as CityCode,
  dbo.FunCarBrandById(t1.CarBrand) as CarBrandName,t1.TJJG,t1.BegTime,t1.TJ,t1.Image,t1.EndTime
  from CarInfo
 ) inner join A
 (select max(id) from b group by CarName) B on A.id=B.id
) t1 left join CompanyShops t2 on (t1.ShopsId=t2.Id) 
order by t1.CarBrand

自己调试下试试

收获园豆:2
尐sんΙ頭 | 初学一级 |园豆:178 | 2012-12-24 15:24
其他回答(3)
0

你的需求是什么,distinct只能针对一个字段,你这样写是不行的

收获园豆:1
Rich.T | 园豆:3440 (老鸟四级) | 2012-12-24 11:49

  我就是想针对CarBrand字段 来 判断数据是否重复啊。

支持(0) 反对(0) 次时代 | 园豆:5 (初学一级) | 2012-12-24 11:52

@次时代: 

删除重复行

delete 表 where id not in(SELECT MAX(id) AS id FROM 表 GROUP BY 字段)

支持(0) 反对(0) Rich.T | 园豆:3440 (老鸟四级) | 2012-12-24 14:33

@次时代: 判断重复的方式很多,不用distinct吧,count一下大于1不就重复了么?

支持(0) 反对(0) 茂茂 | 园豆:2892 (老鸟四级) | 2012-12-24 16:00

@茂茂: 

他是需要删除重复行,不是判断有没有重复的行

支持(0) 反对(0) Rich.T | 园豆:3440 (老鸟四级) | 2012-12-24 16:01
0

你是想怎样?是只要CarBrand重复,不管其它字段重复不重复,都认为是重复,然后在这些重复的记录中随机还是按什么规则选取一条记录。还是说多条记录的多个字段数据相同才算重复?

不过你写的行不通吧。。

收获园豆:1
jone_e | 园豆:1410 (小虾三级) | 2012-12-24 12:35

是滴。只要CarBrand重复,其他字段不管。 假如有两条CarBrand=100    的  就只留其中一条就行~~。我的代码是有问题,所以问问你们

支持(0) 反对(0) 次时代 | 园豆:5 (初学一级) | 2012-12-24 13:53
0

distinct只能针对一个字段,其他字段如果想要一起查询出来就必须按照这个字段区分后能分组,否则会有问题。比如学校里面根据老师distinct,那么其他想要一起查询出来的只能是课程(只教语文的)或者班级(只负责教一个班的)这种跟老师能一起区分的,学生就不能查出来了,因为一个老师带了很多学生,你这个查询把其他项都去掉肯定没问题,但是现在肯定不行的你。要么就多用几个sql语句吧一步一来

收获园豆:1
茂茂 | 园豆:2892 (老鸟四级) | 2012-12-24 13:49

是滴。只要CarBrand重复,其他字段不管。 假如有两条CarBrand=100    的  就只留其中一条就行~~。我的代码是有问题,所以问问你们

支持(0) 反对(0) 次时代 | 园豆:5 (初学一级) | 2012-12-24 13:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册