-- phpMyAdmin SQL Dump
-- version 4.4.15.10
-- https://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 24, 2017 at 02:31 PM
-- Server version: 10.3.1-MariaDB
-- PHP Version: 5.4.16

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!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 utf8mb4 */;

--
-- Database: `easyedi`
--

-- --------------------------------------------------------

--
-- Table structure for table `templatevalues`
--

CREATE TABLE IF NOT EXISTS `templatevalues` (
  `UID` int(10) unsigned NOT NULL,
  `GUID` text NOT NULL,
  `parent_template` text NOT NULL,
  `parent_iteration` int(11) NOT NULL,
  `current_template` text NOT NULL,
  `current_iteration` int(11) NOT NULL,
  `tkey` text NOT NULL,
  `val` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1241 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `templatevalues`
--

INSERT INTO `templatevalues` (`UID`, `GUID`, `parent_template`, `parent_iteration`, `current_template`, `current_iteration`, `tkey`, `val`) VALUES
(1207, '08388e25-33e7-49ec-92ac-456f15e29aca', 'ahtemplate', 0, 'ahtemplate_suborder', 0, 'linnumber', '1'),
(1240, '08388e25-33e7-49ec-92ac-456f15e29aca', '', 0, 'ahtemplate', 0, 'junk4', '0000063671610');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `templatevalues`
--
ALTER TABLE `templatevalues`
  ADD PRIMARY KEY (`UID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `templatevalues`
--
ALTER TABLE `templatevalues`
  MODIFY `UID` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1241;
/*!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 */;

-- The query
WITH recursive temp as (
   SELECT current_template, current_iteration, parent_template, parent_iteration, tkey, val
   FROM templatevalues
   WHERE tkey='linnumber' AND val='1' AND guid='08388e25-33e7-49ec-92ac-456f15e29aca' -- this is the starting point
   UNION ALL
   SELECT c.current_template, c.current_iteration, c.parent_template, c.parent_iteration, c.tkey, c.val
   FROM templatevalues c
   JOIN temp p ON p.parent_template = c.current_template AND p.parent_iteration = c.current_iteration AND guid='08388e25-33e7-49ec-92ac-456f15e29aca' -- this is the recursion
),
temp2 as (
   SELECT current_template, current_iteration, parent_template, parent_iteration, tkey, val
   FROM templatevalues
   WHERE tkey='linnumber' AND val='1' AND guid='08388e25-33e7-49ec-92ac-456f15e29aca' -- this is the starting point
   UNION ALL
   SELECT c.current_template, c.current_iteration, c.parent_template, c.parent_iteration, c.tkey, c.val
   FROM templatevalues c
   JOIN temp2 p ON c.parent_template = p.current_template AND c.parent_iteration = p.current_iteration AND guid='08388e25-33e7-49ec-92ac-456f15e29aca' -- this is the recursion
)
SELECT *
FROM temp2 UNION ALL (SELECT * FROM temp)