mysql prevent displaying a row ONE which has reference in another row TWO but no reference in row THREE

Posted by Jayapal Chandran on Stack Overflow See other posts from Stack Overflow or by Jayapal Chandran
Published on 2011-11-16T08:04:39Z Indexed on 2011/11/22 9:50 UTC
Read the original article Hit count: 371

Filed under:
|

I have a table like the following

id | name     | pid

1  | sam      | NULL

2  | sams ref | 1

3  | pam      | NULL

For the first time the first row gets inserted which will have pid as null I insert a row which is related to the first row and then i insert a row which is new and which may be referred by another row in future.

now i want only the third row to be displayed and not the first and second row as the second row contains the reference of first row.

so if any row has a reference to another row then both the rows should not be displayed.

Only rows which is not having any reference should be displayed.

BESIDES, IS IT A GOOD PRACTICE? PLEASE ADVICE ON THIS.

Edited

When i updated in server the query is always giving empty result. here is what i have

enter image description here

and this one enter image description here

When pid is NULL then that row should appear but when another entry in the same table with pid as its parent id or any other rows id appears then both the rows should not appear.

so if any pid has been referred then both the rows should not appear. here only one row will refer another row and not more than that.

in my localhost i have mysql version 5.0.1 or something like that but when i installed xampp in another system it had 5.5 and in the live server it was 5.3

so in version around 5.0 the query is returning rows but in higher versions it is returning empty rows.

so now i this case how to make a query?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about join