Last active
April 27, 2018 03:33
-
-
Save darkcolonist/345dc2240358d29d81977febe3867619 to your computer and use it in GitHub Desktop.
le group_concat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `buses`; | |
CREATE TABLE `buses` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`schedule_id` int(10) unsigned DEFAULT NULL, | |
`route` varchar(255) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=81; | |
LOCK TABLES `buses` WRITE; | |
INSERT INTO `buses` VALUES (1,3,'1'),(2,39,'2'),(3,26,'3'),(4,15,'4'),(5,30,'5'),(6,4,'6'),(7,6,'7'),(8,16,'8'),(9,25,'9'),(10,32,'10'),(11,34,'11'),(12,5,'12'),(13,39,'13'),(14,39,'14'),(15,43,'15'),(16,48,'16'),(17,27,'17'),(18,42,'18'),(19,43,'19'),(20,33,'20'),(21,39,'21'),(22,41,'22'),(23,1,'23'),(24,5,'24'),(25,14,'25'),(26,19,'26'),(27,19,'27'),(28,48,'28'),(29,18,'29'),(30,8,'30'),(31,7,'31'),(32,6,'32'),(33,35,'33'),(34,39,'34'),(35,23,'35'),(36,43,'36'),(37,45,'37'),(38,2,'38'),(39,36,'39'),(40,28,'40'),(41,22,'41'),(42,36,'42'),(43,16,'43'),(44,27,'44'),(45,46,'45'),(46,15,'46'),(47,40,'47'),(48,26,'48'),(49,41,'49'),(50,43,'50'),(51,1,'51'),(52,20,'52'),(53,24,'53'),(54,9,'54'),(55,30,'55'),(56,36,'56'),(57,22,'57'),(58,25,'58'),(59,35,'59'),(60,48,'60'),(61,37,'61'),(62,27,'62'),(63,10,'63'),(64,14,'64'),(65,6,'65'),(66,13,'66'),(67,37,'67'),(68,15,'68'),(69,29,'69'),(70,33,'70'),(71,29,'71'),(72,39,'72'),(73,10,'73'),(74,48,'74'),(75,44,'75'),(76,33,'76'),(77,46,'77'),(78,1,'78'),(79,18,'79'),(80,34,'80'); | |
UNLOCK TABLES; | |
-- | |
-- Table structure for table `schedules` | |
-- | |
DROP TABLE IF EXISTS `schedules`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `schedules` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`station_id` int(10) unsigned DEFAULT NULL, | |
`schedule` time NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=49; | |
/*!40101 SET character_set_client = @saved_cs_client */; | |
-- | |
-- Dumping data for table `schedules` | |
-- | |
LOCK TABLES `schedules` WRITE; | |
/*!40000 ALTER TABLE `schedules` DISABLE KEYS */; | |
INSERT INTO `schedules` VALUES (1,15,'00:00:00'),(2,9,'00:30:00'),(3,5,'01:00:00'),(4,8,'01:30:00'),(5,9,'02:00:00'),(6,13,'02:30:00'),(7,2,'03:00:00'),(8,12,'03:30:00'),(9,3,'04:00:00'),(10,8,'04:30:00'),(11,9,'05:00:00'),(12,9,'05:30:00'),(13,3,'06:00:00'),(14,7,'06:30:00'),(15,4,'07:00:00'),(16,14,'07:30:00'),(17,5,'08:00:00'),(18,12,'08:30:00'),(19,9,'09:00:00'),(20,4,'09:30:00'),(21,12,'10:00:00'),(22,12,'10:30:00'),(23,13,'11:00:00'),(24,16,'11:30:00'),(25,2,'12:00:00'),(26,11,'12:30:00'),(27,12,'13:00:00'),(28,14,'13:30:00'),(29,5,'14:00:00'),(30,4,'14:30:00'),(31,6,'15:00:00'),(32,14,'15:30:00'),(33,3,'16:00:00'),(34,12,'16:30:00'),(35,14,'17:00:00'),(36,4,'17:30:00'),(37,6,'18:00:00'),(38,12,'18:30:00'),(39,11,'19:00:00'),(40,5,'19:30:00'),(41,11,'20:00:00'),(42,12,'20:30:00'),(43,2,'21:00:00'),(44,14,'21:30:00'),(45,2,'22:00:00'),(46,10,'22:30:00'),(47,16,'23:00:00'),(48,10,'23:30:00'); | |
/*!40000 ALTER TABLE `schedules` ENABLE KEYS */; | |
UNLOCK TABLES; | |
-- | |
-- Table structure for table `stations` | |
-- | |
DROP TABLE IF EXISTS `stations`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `stations` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`name` varchar(255) NOT NULL, | |
`lat` double NOT NULL, | |
`lon` double NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `stations_name_unique` (`name`) | |
) ENGINE=InnoDB AUTO_INCREMENT=19; | |
/*!40101 SET character_set_client = @saved_cs_client */; | |
-- | |
-- Dumping data for table `stations` | |
-- | |
LOCK TABLES `stations` WRITE; | |
/*!40000 ALTER TABLE `stations` DISABLE KEYS */; | |
INSERT INTO `stations` VALUES (1,'Baclaran',1.3472501,103.8544057),(2,'Rotonda',1.3476961,103.8523406),(3,'Magallanes',1.3470359,103.8525302),(4,'Pasay Road',1.3461971,103.8509559),(5,'Ayala',1.3482114,103.8306532),(6,'Buendia',1.3465124,103.8455072),(7,'Estrella',1.3484074,103.8527312),(8,'Guadalupe',1.3549884,103.8502792),(9,'Boni',1.3587214,103.8427692),(10,'Reliance',1.3538304,103.8398512),(11,'Crossing',1.3443484,103.8381342),(12,'J. Vargas',1.3406584,103.8401942),(13,'Ortigas',1.3372264,103.8454732),(14,'Galleria',1.3383844,103.8509662),(15,'White Plains',1.3423744,103.8537982),(16,'Santolan',1.3438284,103.8689442),(17,'Farmers',1.3438284,103.8689442),(18,'Ermin Garcia',1.3427984,103.8822482); | |
/*!40000 ALTER TABLE `stations` ENABLE KEYS */; | |
UNLOCK TABLES; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
GROUP_CONCAT(buses.route) AS le_concat_por_favor, | |
schedules.schedule, | |
stations.name, | |
CONCAT( | |
MOD( | |
TIMESTAMPDIFF(HOUR, NOW(), SCHEDULE), | |
24 | |
), | |
'h', | |
MOD( | |
TIMESTAMPDIFF(MINUTE, NOW(), SCHEDULE), | |
60 | |
), | |
'm' | |
) AS TIMEDIFF | |
FROM | |
buses | |
LEFT JOIN schedules | |
ON (buses.schedule_id = schedules.id) | |
LEFT JOIN stations | |
ON ( | |
schedules.station_id = stations.id | |
) | |
WHERE TIMESTAMPDIFF(MINUTE, NOW(), schedules.schedule) > 0 | |
GROUP BY CONCAT(schedules.schedule, stations.name) | |
ORDER BY stations.name, | |
schedules.schedule | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output be like: