日期:2014-05-18  浏览次数:20813 次

distinct的问题
就是用了distinct,它只能查distinct对应的字段哦,如果你写成:

eg:select   distinct(ebay_id),id,referer         from   autoorder   order   by   id

的话,那查询的将会是autoorder表中的所有记录,没有做到筛选不同ebay_id的记录

但是,如果我用了distinct(ebay_id)来实现筛选不同ebay_id的记录,又需要取出对应的其他字段该怎么写呢?

select   ebay_id,id,referer   from   autoorder   where   ebay_id   in   (select   distinct(ebay_id)   from   autoorder   order   by   id);
这样用子查询可以吗?或者还有没有其他法子呢?

------解决方案--------------------
select a.ebay_id,a.id,a.referer from autoorder a inner join (select distinct(ebay_id) from autoorder order by id) b on a.ebay_id=b.ebay_id

连接
------解决方案--------------------
select ebay_id,id,referer from autoorder a where (select count(*) from autoorder where ebay_id=a.ebay_id and (cast(id as varchar)+referer) <=(cast(a.id as varchar)+a.referer))=1
------解决方案--------------------
select a.ebay_id,a.id,a.referer from autoorder a where exists(select distinct(ebay_id) from autoorder where ebay_id=a.ebay_id)
------解决方案--------------------
无论怎样,只要记录之间这三个字段有一个不同,用distinct也过滤不了
------解决方案--------------------
除非是相同的ebay_id只取一条记录
------解决方案--------------------
这个问题本身就有点问题

用了distinct(ebay_id)来实现筛选不同ebay_id的记录,又需要取出对应的其他字段该怎么写呢?

比如ebay_id为1的时候,有2条记录,distinct ebay_id就为1,但是两条记录,怎么可能在一条记录里体现出来呢,逻辑就有问题嘛