Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 5th, 2012, 07:07 PM
Registered User
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help in SQL Query

Hi everyone.
Getting straight to the point. I have let's suppose two tables with fields,

table a: id, parentid, status
table b: id, name

Now i want to get the data in such a form that if an id is selected, check for it's parent id and if the parent id exists (means not 0), then check that parent id's parent id and recursive call until there is an id which has no parentid.
And then i want to show the results as follow. Like if table a has data:
id parentid status
1 0 a
2 1 a
3 2 a
4 0 a

And table b has data:
id name
1 test1
2 test2
3 test3
4 test4

Now if id 3 is selected i want to show the output like;

And if 4 is selected then;

What i have tried is as follows:
FROM a as one,
a as two LEFT JOIN b ON two.id = b.id
WHERE two.id = "(Here i get the value)"
I am working on this thing since last two days and can not take it to end. So can any of you kindly help me or can guide me on the proper track please???


Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query i need seearam MySQL 7 November 30th, 2008 03:14 AM
Help with C# and SQL query Codeman1234 C# 2005 19 April 24th, 2008 05:26 AM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
sql query plug_samir SQL Server 2000 1 May 17th, 2005 08:26 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.