|
发表于 2020-12-18 13:00:01
|
显示全部楼层
--建表(只建立相关字段)
create table T_Schedule_Detail
(
cid int,
ScheduleCode varchar(4),
cityName varchar(10)
)
insert into T_Schedule_Detail select 1, 'T108', '北京'
insert into T_Schedule_Detail select 2, 'T108', '郑州'
insert into T_Schedule_Detail select 3, 'T108', '长沙'
insert into T_Schedule_Detail select 4, 'T108', '海南'
insert into T_Schedule_Detail select 5, 'T989', '长沙'
insert into T_Schedule_Detail select 6, 'T989', '重庆'
--查询
select a.ScheduleCode ,a.cityname ,b.ScheduleCode , b.cityname
from T_Schedule_Detail a left join T_Schedule_Detail b
on a.cityName = b.cityName and a.ScheduleCode <> b.ScheduleCode
where exists (select 1 from T_Schedule_Detail
where ScheduleCode = a.ScheduleCode and cityName = '北京') and
exists (select 1 from T_Schedule_Detail
where ScheduleCode = b.ScheduleCode and cityName = '重庆')
--结果
ScheduleCode cityname ScheduleCode cityname
------------ ---------- ------------ ----------
T108 长沙 T989 长沙
(所影响的行数为 1 行)
--其意为:乘坐T108次在长沙转T989次 |
|