简体   繁体   中英

Select all products (table1) under parent_id (table2 = categories)

How do I SELECT them from the database to show all products (table1) under parent_id from the categories (table2)?

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

Parent Category ID #1

  • Product#1 > category_id #5
  • Product#2 > category_id #10
  • Product#3 >> category_id #16
  • Product#4 >> category_id #20

Parent Category ID #2

  • Product#5 > category_id #31
  • Product#6 > category_id #33

Parent Category ID #3

  • Product#7 > category_id #27
  • Product#8 > category_id #29

Here's what I have so far, but it's still not showing all the products in the parent category sqlfiddle

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)

DB: categories

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 ;

DB: products

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 ;

Structure for CATEGORIES

  • Kid's Clothing
  • --Big Boys & Girls Apparel
  • -------Tops & Tees
  • -------Skirt & Pants
  • --Baby Boys & Girls
  • -------Rompers/Onesies
  • -------Baby Accessories
  • -------Baby Care & Toys
  • --Clothing Set --------Unisex

Code for viewing the products (example):

$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 '<p>ID#'.$id.'-'.$product.' (Category#'.$cat.'-'.$pcat.')</p>';
}
mysqli_close($con);

Actually as I understand your question, it is that.

You have three different tables.

1st. Parent Category named i keep parent_category(id, name) whereas id is primary key.

2nd. You have child category table, named categories. As you already define in above in your question. I have made one change in your table. i add foreign key to parent_id.

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),
Foreign Key (parent_id) REFERENCES parent_category(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

3rd. You have a product table. I have also add a foreign key to category.

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),
Foreign Key (category) REFERENCES categories(category_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Now You want to view the products from their parent category.

So below is the code.

$getid = 1;
$query = mysql_query("SELECT products.*,categories.* from products, categories, parent_category WHERE products.category=categories.category_id AND parent_category.id=categories.parent_id AND parent_category.id=$getid");
while($record = mysql_fetch_array($query)) {
    echo '<p>ID#'.$record['id'].'-'.$record['product'].' (Category#'.$record['category_id'].'-'.$record['parent_id'].')</p>';
}

That's it.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM