这样可以查找单列车select 车次 from 表pass where 车站=‘上海’ and 车站=‘西安’不知道你考虑转车否?------------------------------------------------ --上面這個語句應該得不到紀錄的... --試試這個 select 车次 From Pass Where 车站 = 上海 and 车次 in (select 车次 From Pass Where 车站 =西安 )
select 车次 from table where 车站 in ('上海','西安') group by 车次 having count(1)=2
对于在一堆数据里找特征数据 首先考虑 group by
select 车次 from table where 车站 in ('上海','西安') group by 车次 having count(1)=2 这个语句有用,不过查询出来的结果没有先后顺序,同时搜出了 上海和西安两个方向的车次
那就稍微改进,可以有几种思路 一种根据原始记录进行join进行筛选select distinct x1.车次 from table x1 join table x2 on x1.车次=x2.车次 and x1.到站<x2.到站 where x1.车次 = '上海' and x2.车次 ='西安'另外一种思路根据group by出来的结果去取车次进行判断== 不过有方向的语句应该没有比简洁的上面说的更简洁了,不做研究
--上面這個語句應該得不到紀錄的...
--試試這個
select 车次 From Pass
Where 车站 = 上海 and 车次 in (select 车次 From Pass Where 车站 =西安 )
from table
where 车站 in ('上海','西安')
group by 车次
having count(1)=2
from table
where 车站 in ('上海','西安')
group by 车次
having count(1)=2
这个语句有用,不过查询出来的结果没有先后顺序,同时搜出了
上海和西安两个方向的车次
一种根据原始记录进行join进行筛选select distinct x1.车次
from table x1 join table x2 on x1.车次=x2.车次 and x1.到站<x2.到站
where x1.车次 = '上海' and x2.车次 ='西安'另外一种思路根据group by出来的结果去取车次进行判断== 不过有方向的语句应该没有比简洁的上面说的更简洁了,不做研究
帮忙!小新谢谢各位了!