mysql实现地铁线路换乘查询

20181221完成了sqlzoo,MARK一下mysql实现地铁线路换乘查询_第1张图片

学(shua)习(ti)过程遇到最后一个JOIN的话题:公交换乘的SQL实现,原来SQL还能这么玩,现实中真的能用到,心情很激动。于是在刷完题的周末后,2018的最后一周,立下flag,创建深圳地铁线路数据库和查询(包含小于等于一次换乘)。说干就干~

流程图

Created with Raphaël 2.2.0 开始 创建数据库subway 创建站点信息表szsubstop和线路信息表szsubway 创建车辆方向表szsubdirect 是否换乘? 换乘一次方案查询 不换乘方案查询 yes no

具体实施

  1. 创建数据库subway
DROP DATABASE IF EXISTS subway;--创建数据库
CREATE DATABASE subway;--创建数据库
USE subway;--使用数据库
  1. 创建站点信息表szsubstop和线路信息表szsubway
    2.1 创建站点信息表szsubstop
-- 创建表格
CREATE TABLE IF NOT EXISTS szsubway (
    ID INT NOT NULL,
    num VARCHAR(10),
    stopid VARCHAR(10));

-- 插入数据
INSERT INTO szsubstop
VALUES ('1','机场东'),('2','后瑞'),('3','固戍'),('4','西乡'),('5','坪洲'),('6','宝体'),('7','宝安中心'),('8','新安'),('9','前海湾'),('10','鲤鱼门'),
('11','大新'),('12','桃园'),('13','深大'),('14','高新园'),('15','白石洲'),('16','世界之窗'),('17','华侨城'),('18','侨城东'),('19','竹子林'),('20','车公庙'),
('21','香蜜湖'),('22','购物公园'),('23','会展中心'),('24','岗厦'),('25','华强路'),('26','科学馆'),('27','大剧院'),('28','老街'),('29','国贸'),('30','罗湖'),
('31','赤湾'),('32','蛇口港'),('33','海上世界'),('34','水湾'),('35','东角头'),('36','湾厦'),('37','海月'),('38','登良'),('39','后海'),('40','科苑'),
('41','红树湾'),('42','侨城北'),('43','深康'),('44','安托山'),('45','侨香'),('46','香蜜'),('47','香梅北'),('48','景田'),('49','莲花西'),('50','福田'),
('51','市民中心'),('52','岗厦北'),('53','华强北'),('54','燕南'),('55','湖贝'),('56','黄贝岭'),('57','新秀'),('58','益田'),('59','石厦'),('60','少年宫'),
('61','莲花村'),('62','华新'),('63','通新岭'),('64','红岭'),('65','晒布'),('66','翠竹'),('67','田贝'),('68','水贝'),('69','草埔'),('70','布吉'),
('71','木棉湾'),('72','大芬'),('73','丹竹头'),('74','六约'),('75','塘坑'),('76','横岗'),('77','永湖'),('78','荷坳'),('79','大运'),('80','爱联'),
('81','吉祥'),('82','龙城广场'),('83','南联'),('84','双龙'),('85','福田口岸'),('86','福民'),('87','少年宫'),('88','莲花北'),('89','上梅林'),('90','民乐'),
('91','白石龙'),('92','深圳北站'),('93','机场东'),('94','红山'),('95','上塘'),('96','龙胜'),('97','龙华'),('98','清湖'),('99','前海湾'),('100','临海'),
('101','宝华'),('102','翻身'),('103','灵芝'),('104','洪浪北'),('105','兴东'),('106','留仙洞'),('107','西丽'),('108','大学城'),('109','塘朗'),('110','长岭陂'),
('111','民治'),('112','五和'),('113','坂田'),('114','杨美'),('115','上水径'),('116','下水径'),('117','长龙'),('118','百鸽笼'),('119','布心'),('120','太安'),
('121','怡景'),('122','西丽湖'),('123','茶光'),('124','珠光'),('125','龙井'),('126','桃源村'),('127','深云'),('128','农林'),('129','上沙'),('130','沙尾'),
('131','皇岗村'),('132','福民'),('133','皇岗口岸'),('134','赤尾'),('135','华强南'),('136','黄木岗'),('137','八卦岭'),('138','红岭北'),('139','笋岗'),('140','洪湖'),
('141','碧头'),('142','松岗'),('143','后亭'),('144','沙井'),('145','马安山'),('146','塘尾'),('147','桥头'),('148','福永'),('149','机场北'),('150','机场'),
('151','碧海湾'),('152','宝安'),('153','南山'),('154','红树湾南'),('155','深湾'),('156','深圳湾公园'),('157','下沙'),('158','香梅'),('159','梅景'),('160','下梅林'),
('161','梅村'),('162','孖岭'),('163','银湖'),('164','泥岗'),('165','园岭'),('166','红岭南'),('167','鹿丹村'),('168','人民南'),('169','向西村'),('170','文锦');

2.2 创建线路信息表szsubway

-- 创建表格
CREATE TABLE IF NOT EXISTS szsubstop
   (stopid VARCHAR(10),
   stopname VARCHAR(20));
   
-- 插入数据
INSERT INTO szsubway
VALUES
(1,'Line1','1'),(2,'Line1','2'),(3,'Line1','3'),(4,'Line1','4'),(5,'Line1','5'),(6,'Line1','6'),(7,'Line1','7'),(8,'Line1','8'),(9,'Line1','9'),(10,'Line1','10'),
(11,'Line1','11'),(12,'Line1','12'),(13,'Line1','13'),(14,'Line1','14'),(15,'Line1','15'),(16,'Line1','16'),(17,'Line1','17'),(18,'Line1','18'),(19,'Line1','19'),(20,'Line1','20'),
(21,'Line1','21'),(22,'Line1','22'),(23,'Line1','23'),(24,'Line1','24'),(25,'Line1','25'),(26,'Line1','26'),(27,'Line1','27'),(28,'Line1','28'),(29,'Line1','29'),(30,'Line1','30'),
(31,'Line2','31'),(32,'Line2','32'),(33,'Line2','33'),(34,'Line2','34'),(35,'Line2','35'),(36,'Line2','36'),(37,'Line2','37'),(38,'Line2','38'),(39,'Line2','39'),(40,'Line2','40'),
(41,'Line2','41'),(42,'Line2','16'),(43,'Line2','42'),(44,'Line2','43'),(45,'Line2','44'),(46,'Line2','45'),(47,'Line2','46'),(48,'Line2','47'),(49,'Line2','48'),(50,'Line2','49'),
(51,'Line2','50'),(52,'Line2','51'),(53,'Line2','52'),(54,'Line2','53'),(55,'Line2','54'),(56,'Line2','27'),(57,'Line2','55'),(58,'Line2','56'),
(59,'Line2','57'),(60,'Line3','58'),
(61,'Line3','59'),(62,'Line3','22'),(63,'Line3','50'),(64,'Line3','60'),(65,'Line3','61'),(66,'Line3','62'),(67,'Line3','63'),(68,'Line3','64'),(69,'Line3','28'),(70,'Line3','65'),
(71,'Line3','66'),(72,'Line3','67'),(73,'Line3','68'),(74,'Line3','69'),(75,'Line3','70'),(76,'Line3','71'),(77,'Line3','72'),(78,'Line3','73'),(79,'Line3','74'),(80,'Line3','75'),
(81,'Line3','76'),(82,'Line3','77'),(83,'Line3','78'),(84,'Line3','79'),(85,'Line3','80'),(86,'Line3','81'),(87,'Line3','82'),(88,'Line3','83'),(89,'Line3','84'),(90,'Line4','85'),
(91,'Line4','86'),(92,'Line4','23'),(93,'Line4','51'),(94,'Line4','87'),(95,'Line4','88'),(96,'Line4','89'),(97,'Line4','90'),(98,'Line4','91'),(99,'Line4','92'),(100,'Line4','93'),
(101,'Line4','94'),(102,'Line4','95'),(103,'Line4','96'),(104,'Line4','97'),(105,'Line4','98'),(106,'Line5','99'),(107,'Line5','100'),(108,'Line5','101'),(109,'Line5','7'),(110,'Line5','102'),
(111,'Line5','103'),(112,'Line5','104'),(113,'Line5','105'),(114,'Line5','106'),(115,'Line5','107'),(116,'Line5','108'),(117,'Line5','109'),(118,'Line5','110'),(119,'Line5','92'),(120,'Line5','111'),
(121,'Line5','112'),(122,'Line5','113'),(123,'Line5','114'),(124,'Line5','115'),(125,'Line5','116'),(126,'Line5','117'),(127,'Line5','70'),(128,'Line5','118'),(129,'Line5','119'),(130,'Line5','120'),
(131,'Line5','121'),(132,'Line5','56'),(133,'Line7','122'),(134,'Line7','107'),(135,'Line7','123'),(136,'Line7','124'),(137,'Line7','125'),(138,'Line7','126'),(139,'Line7','127'),(140,'Line7','44'),
(141,'Line7','128'),(142,'Line7','20'),(143,'Line7','129'),(144,'Line7','130'),(145,'Line7','59'),(146,'Line7','131'),(147,'Line7','132'),(148,'Line7','133'),(149,'Line7','134'),(150,'Line7','135'),
(151,'Line7','53'),(152,'Line7','62'),(153,'Line7','136'),(154,'Line7','137'),(155,'Line7','138'),(156,'Line7','139'),(157,'Line7','140'),(158,'Line7','67'),(159,'Line7','120'),(160,'Line9','154'),
(161,'Line9','155'),(162,'Line9','156'),(163,'Line9','157'),(164,'Line9','20'),(165,'Line9','158'),(166,'Line9','48'),(167,'Line9','159'),(168,'Line9','160'),(169,'Line9','161'),(170,'Line9','89'),
(171,'Line9','162'),(172,'Line9','163'),(173,'Line9','164'),(174,'Line9','138'),(175,'Line9','165'),(176,'Line9','64'),(177,'Line9','166'),(178,'Line9','167'),(179,'Line9','168'),(180,'Line9','169'),
(181,'Line9','170'),(182,'Line11','141'),(183,'Line11','142'),(184,'Line11','143'),(185,'Line11','144'),
(186,'Line11','145'),(187,'Line11','146'),(188,'Line11','147'),(189,'Line11','148'),
(190,'Line11','149'),(191,'Line11','150'),(192,'Line11','151'),(193,'Line11','152'),(194,'Line11','99'),(195,'Line11','153'),(196,'Line11','39'),(197,'Line11','154'),(198,'Line11','20'),(199,'Line11','50');
  1. 创建车辆方向表szsubdirect
CREATE TABLE IF NOT EXISTS szsubdirect
AS
SELECT t.num,t.fwddir,t.bwddir
FROM
	(SELECT DISTINCT sy.num,sy.ID,
		LAST_VALUE(sp.stopname) OVER (PARTITION BY sy.num ORDER BY sy.ID ASC) AS fwddir,
    	FIRST_VALUE(sp.stopname) over (PARTITION BY sy.num ORDER BY sy.ID ASC) AS bwddir
  	FROM szsubway sy 
		JOIN szsubstop sp ON (sy.stopid=sp.stopid)) t
WHERE t.ID IN (SELECT MAX(ID) FROM szsubway GROUP BY num);
  1. 不换乘方案查询不换乘方案查询
-- 不换乘的方案
set @start = '布吉';
set @end = '大学城';

SELECT sp.stopname AS '初始站名称',
	sy.num AS '线路名称',
	CONCAT(IF(sy.ID<sy2.ID, sd.fwddir,sd.bwddir),'方向') AS '乘车方向',
	ABS(sy.ID-sy2.ID) AS '乘车站数',
	sp2.stopname AS '目的地站名称'
FROM szsubway sy 
	JOIN szsubway sy2 ON (sy.num=sy2.num)
	JOIN szsubstop sp ON (sy.stopid=sp.stopid)
	JOIN szsubstop sp2 ON (sy2.stopid=sp2.stopid)
	JOIN szsubdirect sd ON (sy.num=sd.num)
WHERE sp.stopname=@start AND sp2.stopname=@end;

运行结果:
在这里插入图片描述
6. 换乘一次方案查询

-- 换乘一次的方案
set @start = '世界之窗';
set @end = '大学城';
SELECT 
   sp.stopname AS '初始站名称',
   sy.num AS '首乘线路名称',
   CONCAT(IF(sy.ID<sy2.ID,sd.fwddir,sd.bwddir),'方向') AS '首乘方向',
   ABS(sy.ID-sy2.ID) AS '首乘站数',
   sp2.stopname AS '换乘站名称',
   sy4.num AS '换乘线路名称',
   CONCAT(IF(sy4.ID<sy6.ID,sd2.fwddir,sd2.bwddir),'方向') AS '换乘方向',
   ABS(sy4.ID-sy6.ID) AS '换乘站数',
   sp6.stopname AS '目的地名称'
FROM szsubway sy 
   JOIN szsubway sy2 ON (sy.num=sy2.num)
   JOIN szsubstop sp ON (sy.stopid=sp.stopid)
   JOIN szsubstop sp2 ON (sy2.stopid=sp2.stopid)
   JOIN szsubdirect sd ON (sy.num=sd.num)
   JOIN szsubway sy4 ON (sy4.num!=sy2.num AND sy4.stopid=sy2.stopid)
   JOIN szsubway sy6 ON (sy4.num=sy6.num)
   JOIN szsubstop sp4 ON (sy4.stopid=sp4.stopid)
   JOIN szsubstop sp6 ON (sy6.stopid=sp6.stopid)
   JOIN szsubdirect sd2 ON (sy4.num=sd2.num)
WHERE sp.stopname=@start AND sp6.stopname=@end AND sp4.stopname!=@end;

运行结果:
在这里插入图片描述

Have fun~
20181224

你可能感兴趣的:(SQL练习)