Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect all products (table1) under parent_id (table2 = categories)
    primarykey
    data
    text
    <p>How do I SELECT them from the database to show all products (table1) under parent_id from the categories (table2)?</p> <p>For example, I want to show/list all the products under parent_id = 1 (Kid's Clothing) including all products under sub-categories per page using a link like cat.php?id=1</p> <p>Parent Category ID #1</p> <ul> <li>Product#1 > category_id #5</li> <li>Product#2 > category_id #10</li> <li>Product#3 >> category_id #16</li> <li>Product#4 >> category_id #20</li> </ul> <p>Parent Category ID #2</p> <ul> <li>Product#5 > category_id #31</li> <li>Product#6 > category_id #33</li> </ul> <p>Parent Category ID #3</p> <ul> <li>Product#7 > category_id #27</li> <li>Product#8 > category_id #29</li> </ul> <p>Here's what I have so far, but it's still not showing all the products in the parent category <a href="http://sqlfiddle.com/#!2/40e8ed/15" rel="nofollow">sqlfiddle</a></p> <pre><code>SELECT * FROM products LEFT JOIN categories ON products.category = categories.category_id GROUP BY (SELECT parent_id FROM categories WHERE parent_id = 1 GROUP BY parent_id) </code></pre> <p>DB: categories</p> <pre><code>CREATE TABLE IF NOT EXISTS `categories` ( `category_id` int(10) NOT NULL AUTO_INCREMENT, `parent_id` int(10) DEFAULT NULL, `title` varchar(255) NOT NULL, PRIMARY KEY (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; </code></pre> <p>DB: products</p> <pre><code>CREATE TABLE IF NOT EXISTS `products` ( `id` int(10) NOT NULL AUTO_INCREMENT, `product` varchar(255) DEFAULT NULL, `description` longtext DEFAULT NULL, `category` int(10) DEFAULT NULL, `color` varchar(255) DEFAULT NULL, `sizes` varchar(255) DEFAULT NULL, `style` varchar(255) DEFAULT NULL, `material` varchar(255) DEFAULT NULL, `stock` varchar(255) DEFAULT NULL, `ws_price` decimal(6,2) DEFAULT NULL, `rt_price` decimal(6,2) DEFAULT NULL, `sp_code` varchar(255) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; </code></pre> <p>Structure for CATEGORIES</p> <ul> <li>Kid's Clothing</li> <li>--Big Boys &amp; Girls Apparel </li> <li>-------Tops &amp; Tees</li> <li>-------Skirt &amp; Pants</li> <li>--Baby Boys &amp; Girls</li> <li>-------Rompers/Onesies</li> <li>-------Baby Accessories</li> <li>-------Baby Care &amp; Toys</li> <li>--Clothing Set --------Unisex</li> </ul> <p>Code for viewing the products (example):</p> <pre><code>$getid = $_GET['id']; $q = mysqli_query($con," SELECT products.*, categories.* FROM products, categories WHERE products.category = categories.category_id GROUP BY categories.parent_id= $getid "); while($row = mysqli_fetch_array($q, MYSQLI_ASSOC)){ $id = $row['id']; $product = $row['product']; $cat = $row['category']; $c = mysqli_query($con,"SELECT title FROM categories WHERE category_id = $cat"); while($r = mysqli_fetch_array($c)){ $pcat= $r['title']; } echo '&lt;p&gt;ID#'.$id.'-'.$product.' (Category#'.$cat.'-'.$pcat.')&lt;/p&gt;'; } mysqli_close($con); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload