PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete CREATE TABLE menu (`mid` int, `parent` varchar(4), `sub` varchar(4), `org` int, `item` varchar(50), `price` varchar(50), `dsc1` varchar(255), `dsc2` varchar(255)) ; INSERT INTO `menu` (`mid`,`parent`,`sub`,`org`,`item`,`price`,`dsc1`,`dsc2`) VALUES (1,NULL,NULL,1,'Apple',NULL,NULL,NULL); INSERT INTO `menu` (`mid`,`parent`,`sub`,`org`,`item`,`price`,`dsc1`,`dsc2`) VALUES (2,1,NULL,1,'iPhone ','$500','(HI)',NULL); INSERT INTO `menu` (`mid`,`parent`,`sub`,`org`,`item`,`price`,`dsc1`,`dsc2`) VALUES (3,1,2,1,'iPhone Plus','$600','(HI)',NULL); INSERT INTO `menu` (`mid`,`parent`,`sub`,`org`,`item`,`price`,`dsc1`,`dsc2`) VALUES (4,1,NULL,1,'MacBook','$900','(HI)',NULL); INSERT INTO `menu` (`mid`,`parent`,`sub`,`org`,`item`,`price`,`dsc1`,`dsc2`) VALUES (5,1,4,1,'MacBook Pro','$1200','(HI)',NULL);
Copy Clear
Copy Format Clear
<?php $stmt = $pdo->query(" SELECT `parent_menu`.`item` as `parent_item`, `menu`.`price`, `menu`.`item`, `sub_menu`.`price` as `sub_item_price`, `sub_menu`.`item` as `sub_item` FROM `menu` -- Join the parent brands in if there are any JOIN `menu` as `parent_menu` ON `menu`.`parent` = `parent_menu`.`mid` -- Join in the subs JOIN `menu` as `sub_menu` ON `menu`.`mid` = `sub_menu`.`sub`"); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { echo implode(' ', $row) . "\n"; }
Show:  
Copy Clear