I have a category table in MySql database that holds the following data.
id name parent
-------------------------------------
1 Web development 0
2 Application development 0
3 Linux 0
4 Misc 0
5 Php 1
6 Mysql 1
7 Javascript 1
8 CSS 1
9 C plus plus 2
10 wxWidgets 2
11 Tutorials 3
12 My thoughts 4
13 Java 1
14 JSP 13
15 Spring 14
16 Spring 3.0 15
17 Spring 3.1 15
18 JSF 13
19 Oracle 0
20 Oracle 8i 19
21 Oracle 91 19
22 JSF 2.0 18
23 JSF 2.2 18
I need the following output.
5 Php 1
9 C plus plus 2
11 Tutorials 3
12 My thoughts 4
6 Mysql 1
7 Javascript 1
8 CSS 1
10 wxWidgets 2
22 JSF 2.0 18
23 JSF 2.2 18
16 Spring 3.0 15
17 Spring 3.1 15
20 Oracle 8i 19
21 Oracle 91 19
The result set should contain only the last level of each category. This can be done using a function in MySql. but is there a way to achieve this using an SQL query only?
please check it .. It will work