求一个SQL查询语句 在线等答案

cdh520fy 2008-12-08 11:26:30
select a.line_id from
(select line_id from station where station_name = '商城') a,
(select line_id from station where station_name = '第二中学') b
where a.line_id = b.line_id

我从A表中查得a.line_id
然后想要a.line_id当做B表的where条件
也就是 select * where id = a.line_id

请问怎么把这两个查询语句合并 ?
我是在c#中使用 需要查得数据放到DataGridView里
...全文
60 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdh520fy 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 josy 的回复:]
SQL codeselect *
from B
where id in(
select line_id
from station a
where station_name = '第二中学'
and exists(select * from station where line_id=a.line_id and station_name = '商城')
)

这样?

[/Quote]

OK 完全正确 谢谢
百年树人 2008-12-09
  • 打赏
  • 举报
回复
select * 
from B
where id in(
select line_id
from station a
where station_name = '第二中学'
and exists(select * from station where line_id=a.line_id and station_name = '商城')
)

这样?
cdh520fy 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lintf1986 的回复:]
理解错了,就是要两个站点确定一条路线吧,1楼的正解
[/Quote]

对啊 但是他那也不是我要的结果 从A表中查询出a.line_id 然后当做where条件再在B表中查询出我想要的消息

2个语句分别我能写 但是要怎么合并?

1. (select a.line_id from
(select line_id from station where station_name = '商城') a,
(select line_id from station where station_name = '第二中学') b
where a.line_id = b.line_id)

//得到a.line_id

2. select line_id,startStation,endStation,firstTime,lastTime from linelist where id = a.line_id

//把a.line_id当做where条件 查得我要的 line_id,startStation,endStation,firstTime,lastTime 等信息

因为要把查得的数据放进c#的DataGridView中 所以要把两个语句合并?还是另有他法?
lintf1986 2008-12-08
  • 打赏
  • 举报
回复
理解错了,就是要两个站点确定一条路线吧,1楼的正解
cdh520fy 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 cdh520fy 的帖子:]
select a.line_id from
(select line_id from station where station_name = '商城') a,
(select line_id from station where station_name = '第二中学') b
where a.line_id = b.line_id

我从A表中查得a.line_id 
然后想要a.line_id当做B表的where条件
也就是  select * where id = a.line_id

请问怎么把这两个查询语句合并  ?
我是在c#中使用  需要查得数据放到DataGridView里
[/Quote]

再详细说下我的意思:
A表中查得a.line_id
select a.line_id from
(select line_id from station where station_name = '商城') a,
(select line_id from station where station_name = '第二中学') b
where a.line_id = b.line_id

然后想要a.line_id当做B表的where条件
也就是  select * from B where id = a.line_id

请问怎么把这两个查询语句合并  ?
我是在c#中使用  需要查得数据放到DataGridView里
cdh520fy 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lintf1986 的回复:]
SQL codeselect a.line_id
from a,b
where a.line_id = b.line_id and station_name in ('第二中学','商城')

[/Quote]

提示:对象名“a”无效
cdh520fy 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
SQL codeselect *
from station a
where station_name = '第二中学'
and exists(select * from station where line_id=a.line_id and station_name = '商城')

[/Quote]

得出的结果不是我想要的啊 ?
lintf1986 2008-12-08
  • 打赏
  • 举报
回复
select a.line_id 
from a,b
where a.line_id = b.line_id and station_name in ('第二中学','商城')
百年树人 2008-12-08
  • 打赏
  • 举报
回复
select * 
from station a
where station_name = '第二中学'
and exists(select * from station where line_id=a.line_id and station_name = '商城')

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧