Skip to content

Instantly share code, notes, and snippets.

@oscarr-reyes
Last active October 7, 2016 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarr-reyes/3a9a5e9800f55cfc59569306d40cc98f to your computer and use it in GitHub Desktop.
Save oscarr-reyes/3a9a5e9800f55cfc59569306d40cc98f to your computer and use it in GitHub Desktop.
-- MySQL dump 10.13 Distrib 5.6.31, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: tree
-- ------------------------------------------------------
-- Server version 5.6.31-0ubuntu0.15.10.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `bill_has_bill`
--
DROP TABLE IF EXISTS `bill_has_bill`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bill_has_bill` (
`ancestor` int(10) unsigned NOT NULL AUTO_INCREMENT,
`descendant` int(10) unsigned NOT NULL,
`length` int(11) NOT NULL,
PRIMARY KEY (`ancestor`,`descendant`),
KEY `fk_bill_has_bill_bill2_idx` (`descendant`),
KEY `fk_bill_has_bill_bill1_idx` (`ancestor`),
CONSTRAINT `fk_bill_has_bill_bill1` FOREIGN KEY (`ancestor`) REFERENCES `bill` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_bill_has_bill_bill2` FOREIGN KEY (`descendant`) REFERENCES `bill` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `bill_has_bill`
--
LOCK TABLES `bill_has_bill` WRITE;
/*!40000 ALTER TABLE `bill_has_bill` DISABLE KEYS */;
INSERT INTO `bill_has_bill` VALUES (1,1,0),(1,2,1),(1,3,1),(1,4,2),(1,5,2),(1,6,2),(1,7,2),(1,8,3),(1,9,3),(1,10,3),(1,11,3),(1,12,3),(2,2,0),(2,4,1),(2,5,1),(2,8,2),(2,9,2),(2,10,2),(2,11,2),(3,3,0),(3,6,1),(3,7,1),(3,12,2),(4,4,0),(4,8,1),(4,9,1),(5,5,0),(5,10,1),(5,11,1),(6,6,0),(6,12,1),(7,7,0),(8,8,0),(9,9,0),(10,10,0),(11,11,0),(12,12,0);
/*!40000 ALTER TABLE `bill_has_bill` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-10-06 22:11:53
select max(length) from bill_has_bill where ancestor = 1;
select count(*) from bill_has_bill where ancestor = 1 AND length = 3;
/*Find all Parents with childs*/
SELECT ancestor, COUNT(ancestor) childs FROM bill_has_bill WHERE length = 1 AND descendant in (
SELECT descendant FROM bill_has_bill WHERE ancestor = 1 AND length = 3)
GROUP BY ancestor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment