查询统计记录问题?
地名 顺序
A地 1
B地 2
C地 3
D地 4
E地 5
F地 6
G地 7
H地 8
现要实现:我任意输入两个地点,要查找之间的所有地方。这个查询怎么实现。我用between 不起作用, 谢谢。
[解决办法]
select * from 表
where 顺序 between (select 顺序 from 表 where 地名= 'B地 ')
and (select 顺序 from 表 where 地名= 'D地 ')
[解决办法]
//借用一下楼上代码
应该是这样吧:
select * from 表
where (顺序 between (select 顺序 from 表 where 地名= 'B地 ')
and (select 顺序 from 表 where 地名= 'D地 '))
or
(顺序 between (select 顺序 from 表 where 地名=D地 ')
and (select 顺序 from 表 where 地名= 'B地 '))
[解决办法]
select * from 表
where 顺序 between (select min(顺序) from 表 where 地名 in( 'B地 ', 'D地 '))
and (select max(顺序) from 表 where 地名 in( 'B地 ', 'D地 '))
[解决办法]
if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(地名 varchar(10),顺序 int)
insert into tb(地名,顺序) values( 'A地 ', 1)
insert into tb(地名,顺序) values( 'B地 ', 2)
insert into tb(地名,顺序) values( 'C地 ', 3)
insert into tb(地名,顺序) values( 'D地 ', 4)
insert into tb(地名,顺序) values( 'E地 ', 5)
insert into tb(地名,顺序) values( 'F地 ', 6)
insert into tb(地名,顺序) values( 'G地 ', 7)
insert into tb(地名,顺序) values( 'H地 ', 8)
go
declare @地名1 as varchar(10)
declare @地名2 as varchar(10)
set @地名1 = 'B地 '
set @地名2 = 'H地 '
select * from tb where 顺序 > = (select 顺序 from tb where 地名 = @地名1) and 顺序 <= (select 顺序 from tb where 地名 = @地名2)
drop table tb
/*
地名 顺序
---------- -----------
B地 2
C地 3
D地 4
E地 5
F地 6
G地 7
H地 8
(所影响的行数为 7 行)
*/
[解决办法]
--结果如下:
/*
B地22
C地33
D地44
E地55
F地66
C地210
D地311
E地412
F地513
*/
[解决办法]
--try
if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(id INT,地名 varchar(10),顺序 int,Aid int)
insert into tb(id,地名,顺序,Aid) values(1, 'A地 ', 1,1)
insert into tb(id,地名,顺序,Aid) values(1, 'B地 ', 2,2)
insert into tb(id,地名,顺序,Aid) values(1, 'C地 ', 3,3)
insert into tb(id,地名,顺序,Aid) values(1, 'D地 ', 4,4)
insert into tb(id,地名,顺序,Aid) values(1, 'E地 ', 5,5)
insert into tb(id,地名,顺序,Aid) values(1, 'F地 ', 6,6)
insert into tb(id,地名,顺序,Aid) values(1, 'G地 ', 7,7)
insert into tb(id,地名,顺序,Aid) values(1, 'H地 ', 8,8)
insert into tb(id,地名,顺序,Aid) values(2, 'B地 ', 1,9)
insert into tb(id,地名,顺序,Aid) values(2, 'C地 ', 2,10)
insert into tb(id,地名,顺序,Aid) values(2, 'D地 ', 3,11)
insert into tb(id,地名,顺序,Aid) values(2, 'E地 ', 4,12)
insert into tb(id,地名,顺序,Aid) values(2, 'F地 ', 5,13)
go
--select * from tb
declare @地名1 as varchar(10)
declare @地名2 as varchar(10)
declare @顺序1 as varchar(10)
declare @顺序2 as varchar(10)
set @地名1 = 'B地 '
set @地名2 = 'F地 '
if (select min(Aid) from tb where 地名 = @地名1) <=(select min(Aid) from tb where 地名 = @地名2)
begin
select @顺序1=min(Aid) from tb where 地名 = @地名1
select @顺序2=min(Aid) from tb where 地名 = @地名2
select id,count(*)as 经过地方数 from tb where 顺序 between @顺序1 and @顺序2 group by id
end
if (select min(Aid) from tb where 地名 = @地名1)> =(select min(Aid) from tb where 地名 = @地名2)
begin
select @顺序1=min(Aid) from tb where 地名 = @地名1
select @顺序2=min(Aid) from tb where 地名 = @地名2
select id,count(*)as 经过地方数 from tb where 顺序 between @顺序2 and @顺序1 group by id
end
drop table tb
[解决办法]
create table tb(线路 int,地名 varchar(10),顺序 int)
insert into tb(线路,地名,顺序) values(1, 'A地 ', 1)
insert into tb(线路,地名,顺序) values(1, 'B地 ', 2)
insert into tb(线路,地名,顺序) values(1, 'C地 ', 3)
insert into tb(线路,地名,顺序) values(1, 'D地 ', 4)
insert into tb(线路,地名,顺序) values(1, 'E地 ', 5)
insert into tb(线路,地名,顺序) values(1, 'F地 ', 6)
insert into tb(线路,地名,顺序) values(1, 'G地 ', 7)
insert into tb(线路,地名,顺序) values(1, 'H地 ', 8)
insert into tb(线路,地名,顺序) values(2, 'C地 ', 1)
insert into tb(线路,地名,顺序) values(2, 'D地 ', 2)
insert into tb(线路,地名,顺序) values(2, 'E地 ', 3)
insert into tb(线路,地名,顺序) values(2, 'H地 ', 5)
insert into tb(线路,地名,顺序) values(2, 'I地 ', 6)
insert into tb(线路,地名,顺序) values(3, 'H地 ', 1)
insert into tb(线路,地名,顺序) values(3, 'I地 ', 2)
insert into tb(线路,地名,顺序) values(3, 'J地 ', 3)
insert into tb(线路,地名,顺序) values(3, 'K地 ', 4)
declare @地名1 as varchar(10)
declare @地名2 as varchar(10)
declare @table table(线路 int,顺序 int)
declare @tables table(线路 int,顺序 int)
declare @tabless table(线路 int,顺序 int)
set @地名1 = 'g地 '
set @地名2 = 'c地 '
insert into @table
select 线路,顺序 from tb as a where 地名=@地名1 and 线路=a.线路
insert into @tables
select 线路,顺序 from tb where 地名=@地名2 and 线路 in (select 线路 from @table )
insert into @tabless
select 线路,顺序 from @tables union
select 线路,顺序 from @table where 线路 in (select 线路 from @tables)
select 线路,max(顺序)-min(顺序) as 经过站点数 from @tabless as b where 线路=b.线路
group by 线路
有事去了今天写了下 通过测试