How many stops are in the database.
select count(*) from stops
Find the id value for the stop ‘Craiglockhart’
select id from stops where name = 'Craiglockhart'
Give the id and the name for the stops on the ‘4’ ‘LRT’ service.
select a.id,a.name from stops as a inner join route as b on a.id = b.stop where b.company = 'LRT' and b.num = 4
The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the output to these two routes.
SELECT company, num, COUNT(*) FROM route WHERE stop=149 OR stop=53 GROUP BY company, num having count(*) > 1
Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.
SELECT a.company, a.num, a.stop, b.stop FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num) join stops c on b.stop = c.id WHERE a.stop=53 and c.name = 'London Road'
The query shown is similar to the previous one, however by joining two copies of the stops table we can refer to stops by name rather than by number. Change the query so that the services between ‘Craiglockhart’ and ‘London Road’ are shown. If you are tired of these places try ‘Fairmilehead’ against ‘Tollcross’
SELECT a.company, a.num, stopa.name, stopb.name FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num) JOIN stops stopa ON (a.stop=stopa.id) JOIN stops stopb ON (b.stop=stopb.id) WHERE stopa.name='Craiglockhart' and stopb.name = 'London Road' -- stopa.name='Fairmilehead' and stopb.name = 'Tollcross'
Give a list of all the services which connect stops 115 and 137 (‘Haymarket’ and ‘Leith’)
select distinct a.company,a.num from route a join route b on (a.company = b.company and a.num = b.num) where a.stop = 115 and b.stop = 137
Give a list of the services which connect the stops ‘Craiglockhart’ and ‘Tollcross’
select distinct a.company,a.num from route a inner join route b on (a.company,a.num) = (b.company,b.num) inner join stops c on a.stop = c.id inner join stops d on b.stop = d.id where c.name = 'Craiglockhart' and d.name = 'Tollcross'
Give a distinct list of the stops which may be reached from ‘Craiglockhart’ by taking one bus, including ‘Craiglockhart’ itself, offered by the LRT company. Include the company and bus no. of the relevant services.
select d.name,a.company,a.num from route as a join route as b on (a.company,a.num) = (b.company,b.num) join stops as c on a.stop = c.id join stops as d on b.stop = d.id where c.name = 'Craiglockhart'
Find the routes involving two buses that can go from Craiglockhart to Sighthill.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.Hint
Self-join twice to find buses that visit Craiglockhart and Sighthill, then join those on matching stops.
主要思路:
- 先找出以”Craiglockhart”为开始站的结果集(返回开始路线号,公司名,结束站(start.stop))
- 再找出以”Sighthill”为结束站的结果集(返回开始路线号,公司名,开始站(end.start))
- 连接2个结果集(连接条件: 结束站(start.stop)与开始站(end.start)相同此时成为中间转一次车的一条线路)
select distinct start.num,start.company,name,end.num,end.company from ( select a.num,a.company,b.stop from route as a join route as b on (a.company,a.num) = (b.company,b.num) and a.stop != b.stop where a.stop = ( select id from stops where name = 'Craiglockhart' ) ) as start join ( select c.num,c.company,c.stop from route as c join route as d on (c.company,c.num) = (d.company,d.num) and c.stop != d.stop where d.stop = ( select id from stops where name = 'Sighthill' ) ) as end on start.stop = end.stop join stops on start.stop = stops.id