单行子查询返回多于一行,如何处理。谢谢
select a,b,... from a,b where a.id=b.id and a.id2||''||b.id2=
(select a.id2||''||b.id2 from a,b where a.id=b.id group by a.id2,b.id2 having(count(*))>1)
问题点数:20、回复次数:16Top
1 楼miaoyuli(恶魔厕所里的神)回复于 2005-08-03 15:10:59 得分 0
不用=,用in呢?Top
2 楼wchunge(山子)回复于 2005-08-03 15:14:35 得分 0
用in 太慢。不行Top
3 楼bzszp(SongZip)回复于 2005-08-03 15:17:19 得分 0
select a,b,... from a,b where a.id=b.id and a.id2||''||b.id2=
(select max(a.id2||''||b.id2) from a,b where a.id=b.id group by a.id2,b.id2 having(count(*))>1)
Top
4 楼precipitant(塞北的雪)回复于 2005-08-03 15:30:13 得分 0
如果不用in,好像非常不合理。Top
5 楼wchunge(山子)回复于 2005-08-03 15:37:13 得分 0
bzszp(SongZip) 不行,还是样的提示Top
6 楼miaoyuli(恶魔厕所里的神)回复于 2005-08-03 15:39:07 得分 0
本来这种||后的查询就慢吧?
要不就用笨方法,建临时表,临时表上建索引,先插再查Top
7 楼wchunge(山子)回复于 2005-08-03 15:57:58 得分 0
因为必须用两个字段合起来判断数据才准确Top
8 楼Goldrush(上天有好生之德)回复于 2005-08-03 21:19:05 得分 0
select a,b,... from a,b where a.id=b.id and a.id2||''||b.id2=
(select a.id2||''||b.id2 from a,b where a.id=b.id group by a.id2,b.id2 having(count(*))>1
and rownum='1')Top
9 楼Goldrush(上天有好生之德)回复于 2005-08-03 21:37:04 得分 0
sorry,不对Top
10 楼lwty(藏浪)回复于 2005-08-04 01:17:05 得分 0
select a,b,... from a,b where a.id=b.id and a.id2||''||b.id2=
(select a.id2||''||b.id2 from a,b where a.id=b.id group by a.id2,b.id2 having count(*)>1
and rownum<2)Top
11 楼wchunge(山子)回复于 2005-08-04 08:57:48 得分 0
:lwty(藏浪 提示不是GROUP BY的表达试Top
12 楼s198127(xyz)回复于 2005-08-04 09:31:00 得分 0
同意lwty(藏浪)的语句,只是他的顺序写反了:
select a,b,... from a,b where a.id=b.id and a.id2||''||b.id2=
(select a.id2||''||b.id2 from a,b where a.id=b.id
and rownum<2
group by a.id2,b.id2 having count(*)>1
)Top
13 楼bobfang(匆匆过客)回复于 2005-08-04 10:27:14 得分 0
从楼主写的语句看,你是想查出所有a表中ID不唯一或者b表中ID不唯一的记录。可以这样写:
select a,b,...
from a, b,
(select id from a group by id having count(*)>1
union
select id from b group by id having count(*)>1) c
where a.id=b.id
and a.id=c.id;Top
14 楼wchunge(山子)回复于 2005-08-04 14:18:16 得分 0
bobfang(匆匆过客)
是二个字段合起查出重复记录Top
15 楼bobfang(匆匆过客)回复于 2005-08-04 14:37:52 得分 0
不好意思,看错了,把ID2也看成ID了。这样写
select a,b,...
from a, b,
(select a.id2||''||b.id2 ID2 from a,b
where a.id=b.id
group by a.id2,b.id2
having count(*)>1) c
where a.id=b.id
and a.id2||''||b.id2=c.id2;
不过a.ID2||''||b.ID2与a.ID2||b.ID2是一样的。Top
16 楼bobfang(匆匆过客)回复于 2005-08-04 14:40:45 得分 20
而且上面的语句就等于
select a,b,...
from a, b,
(select a.id2 aid2, b.id2 bID2 from a,b
where a.id=b.id
group by a.id2,b.id2
having count(*)>1) c
where a.id=b.id
and a.id2=c.aid2 and b.id2=c.bid2;Top




