For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > December 2005 > Fetching rows comparing values in same table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Fetching rows comparing values in same table
FatCoin

2005-12-08, 7:07 pm

Hi.
I had one table organized like

entry_id (int)
parent (int)

so i need to create menu from database.
Every entry that parent = 0 is topmost level of menu. Every entry that
has no child is article.
I write recursive function that fetch rows but I need to test every row
in db, so if any of you know how to fetch only that rows that has
children (other row parent is row id).

Also, it would be great if is MySQL 4 (but any solution is great)
compatible.

Thanx.
J.O. Aho

2005-12-08, 7:07 pm

FatCoin wrote:
> Hi.
> I had one table organized like
>
> entry_id (int)
> parent (int)
>
> so i need to create menu from database.
> Every entry that parent = 0 is topmost level of menu. Every entry that
> has no child is article.
> I write recursive function that fetch rows but I need to test every row
> in db, so if any of you know how to fetch only that rows that has
> children (other row parent is row id).


SELECT DISTINCT parent FROM table WHERE parent>0;

This will give you all parents and each parent should be in the list just once
too.


//Aho
FatCoin

2005-12-09, 4:09 am

J.O. Aho wrote:
> FatCoin wrote:
>
>
>
> SELECT DISTINCT parent FROM table WHERE parent>0;
>
> This will give you all parents and each parent should be in the list just once
> too.
>
>
> //Aho


No subqueries!? Simple solution is the best solution, thank you.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com