0

I had a category table as follows

id     |    name       |  parent_of    |   created_on
-------+---------------+---------------+---------------------
1      |   Name 1      |  0            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
2      |   Name 2      |  0            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
3      |   Name 3      |  1            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
4      |   Name 4      |  1            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
5      |   Name 5      |  3            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
6      |   Name 6      |  3            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------

As in the table some category has child categories.

In this table as follows. Specifying the table id

1
  |--> 3
  |   |--> 5
  |   |--> 6
  |--> 4
2

My question is while adding a product, need to select the category for each product. But only need to show the end category. ie need not showing id 1 and 3 since it is not the end. Only need to show 5,6,4 and 2 ( those are the categories with out any category according the list )

Can any one please help me with the MySQL query for listing the category drop down ?

Thanks in advance

2 Answers 2

2

I hope that, this is what you want.

SELECT id FROM category where ID NOT IN (SELECT DISTINCT parent_of FROM category) ORDER BY id DESC
Sign up to request clarification or add additional context in comments.

5 Comments

yes .. that worked .. thanks... but how to make a list as shown ? that tree ? can you please help ?
the tree for complete navigation through categories. Like I mentioned in my question
yes ... that showing the end category working fine ... but I need to show a tree like above ... can you please help ?
then your have to frame an array like this, array(1=>array(3,4), 2, 3=>array(5,6), 4, 5, 6);
0
SELECT c.* FROM categories c 
LEFT JOIN categories c1 ON c.id = c1.parent_of
WHERE c1.id IS NULL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.