表结构如下
t_lx_zd
id lxid fzh zdid (对应t_zhand id)
1 4 1 5
2 4 1 5
3 3 1 5
4 3 1 3t_chec
id lxid fzh chech ftime
1 4 1 WXSH01 9:00
2 4 1 WXSH01 10:00
3 4 1 WXYY03 11:09
4 4 1 JYSH01 18:00
5 2 1 WXJJ09 17:00
t_zhand
id Zname
1 常熟汽车站
2 靖江汽车站
...现在我需要查出车次号chech,发车时间ftime,到达站zname
我需要根据t_chec表查到lxid,fzh ,然后根据这两项到t_lx_zd得到站点编号zdid
然后根据zdid和t_zhand id匹配 然后得到站点名称
但具体怎么写T_SQL语句?
3Q
t_lx_zd
id lxid fzh zdid (对应t_zhand id)
1 4 1 5
2 4 1 5
3 3 1 5
4 3 1 3t_chec
id lxid fzh chech ftime
1 4 1 WXSH01 9:00
2 4 1 WXSH01 10:00
3 4 1 WXYY03 11:09
4 4 1 JYSH01 18:00
5 2 1 WXJJ09 17:00
t_zhand
id Zname
1 常熟汽车站
2 靖江汽车站
...现在我需要查出车次号chech,发车时间ftime,到达站zname
我需要根据t_chec表查到lxid,fzh ,然后根据这两项到t_lx_zd得到站点编号zdid
然后根据zdid和t_zhand id匹配 然后得到站点名称
但具体怎么写T_SQL语句?
3Q
SELECT zdid FROM t_lx_zd T1 RGIHT JOIN t_chec T2 ON T1.lxid = T2.lxid AND T1.fzh = T2.fzh )
insert into @t_lx_zd
select 1,4,1,5 union all
select 2,4,1,5 union all
select 3,3,1,5 union all
select 4,2,1,3--select * from @t_lx_zddeclare @t_chec table (id int,lxid int,fzh int,chech varchar(6),ftime datetime)
insert into @t_chec
select 1,4,1,'WXSH01','9:00' union all
select 2,4,1,'WXSH02','10:00' union all
select 3,4,1,'WXYY03','11:09' union all
select 4,4,1,'JYSH01','18:00' union all
select 5,2,1,'WXJJ09','17:00'--select * from @t_checdeclare @t_zhand table (id int,Zname varchar(10))
insert into @t_zhand
select 1,'常熟汽车站' union all
select 2,'靖江汽车站' union all
select 3,'常江汽车站' union all
select 5,'哈尔滨车站'--select * from @t_zhand
select aa.*,c.Zname from (
select distinct a.id,a.chech,a.ftime,b.zdid from @t_chec a left join @t_lx_zd b on a.lxid=b.lxid and a.fzh=b.fzh
) aa left join
@t_zhand c on aa.zdid=c.id order by aa.id/*
id chech ftime zdid Zname
----------- ------ ----------------------- ----------- ----------
1 WXSH01 1900-01-01 09:00:00.000 5 哈尔滨车站
2 WXSH02 1900-01-01 10:00:00.000 5 哈尔滨车站
3 WXYY03 1900-01-01 11:09:00.000 5 哈尔滨车站
4 JYSH01 1900-01-01 18:00:00.000 5 哈尔滨车站
5 WXJJ09 1900-01-01 17:00:00.000 3 常江汽车站
*/