php - How can I create this mysql "SELECT" query? -
i have 2 mysql tables named 'categories' , 'products'.
these data of tables.
mysql> select * categories; +-------------+--------+----------------------+-------------+ | category_id | parent | name | description | +-------------+--------+----------------------+-------------+ | 1 | null | products | null | | 2 | 1 | computers | null | | 3 | 2 | laptops | null | | 4 | 2 | desktop computers | null | | 5 | 2 | tab pcs | null | | 6 | 2 | crt monitors | null | | 7 | 2 | lcd monitors | null | | 8 | 2 | led monitors | null | | 9 | 1 | mobile phones | null | | 10 | 9 | lg phone | null | | 11 | 9 | anroid phone | null | | 12 | 9 | windows mobile | null | | 13 | 9 | ipad | null | | 14 | 9 | samsung galaxy | null | | 15 | 1 | digital cameras | null | | 16 | 1 | printers , toners | null | | 22 | 1 | computer accessaries | null | | 23 | 22 | usb cables | null | | 24 | 22 | network cables | null | +-------------+--------+----------------------+-------------+ 24 rows in set (0.00 sec) mysql> select product_id, category_id products; +------------+-------------+ | product_id | category_id | +------------+-------------+ | 1 | 24 | | 2 | 6 | | 3 | 6 | | 4 | 6 | +------------+-------------+ 4 rows in set (0.05 sec)
now need create select query products each category. have have category_id
.
this how tried it:
select * products category_id = 6 order added_date desc
my question when creating select query need products if 1 category have subcategories. mean, if category_id 2, need products including sub categories.
can tell me how create select query?
thank you.
select * products category_id = 2 or category_id in (select category_id categories parent = 2) order added_date desc
Comments
Post a Comment