CodeIgniter Forums
Please..... Help.... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Please..... Help.... (/showthread.php?tid=15355)



Please..... Help.... - El Forum - 02-02-2009

[eluser]Bluemagix[/eluser]
Hello,
I have table called category
having fields, records like this

id name parentid
1 Bank 0
2 Shopping 0
3 Axis 1
4 ICICI 1
5 SBI 1
6 Nike 2
7 xyz 2

where parentid 0 means major categories.
and for other is shows the sub category according to parentid

Eg: icici's parentid is 1 because it comes under bank [having id]

Now, i want result as
Bank
Axis
ICICI
SBI
Shopping
Nike
xyz


How can i do that??
which query i should use??


Please..... Help.... - El Forum - 02-02-2009

[eluser]davidbehler[/eluser]
try something like this:
Code:
select *, CASE parentid when "0" then id else parentid
END as order_by
from category
order by order_by ASC, parentid ASC

that's untested and might need some tweaking, but in my opinion something like this should work for you.