If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select from same table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-07-08, 02:36
internationalist internationalist is offline
Registered User
 
Join Date: Jan 2008
Posts: 17
Select from same table

Hi I have the following situation:

tbl_resource

Code:
Id       -    parent_name         - childOf
2                PC                   null
3                Office               null
4                Keyboard              2
I have the following query:
Code:
select * from tbl_resource
I will like to get parent_name for childOf value, so something like:
Code:
Select * from tbl_resource (if childOf != null ) 
{ get parent_name for childOf value)
How do I do this in a query?
Reply With Quote
  #2 (permalink)  
Old 02-07-08, 03:55
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
something like
select <my,column,list> from <mytablename> as A left join <mytablename> as B on B.childof = A.id;

should do the trick, although that will only find the immediate parent

HTH
Reply With Quote
  #3 (permalink)  
Old 02-07-08, 09:39
internationalist internationalist is offline
Registered User
 
Join Date: Jan 2008
Posts: 17
This is not exactly the solution I am looking for, because the table contains much data.

any other alternative?
Reply With Quote
  #4 (permalink)  
Old 02-07-08, 10:12
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
?
that will return all parent ID's, including those with no parentID's
to limit the results apply an appropriate 'where' clause
and / or alter the join characteristic
a simple join will not return those with no parentID
Reply With Quote
  #5 (permalink)  
Old 02-07-08, 10:34
internationalist internationalist is offline
Registered User
 
Join Date: Jan 2008
Posts: 17
Code:
Select A.ID,A.Parent_name,A.ChildOf from testen as A left join testen as B on B.childof = A.ID;
Gives:
Code:
ID	Parent_name	ChildOf
-------------------------------------
1	PC	           NULL
2	Office	           NULL
3	Keyboard	   2
I would like to have something like:
Code:
ID	Parent_name	ChildOf     ChildParentName
------------------------------------------------------
1	PC	           NULL           NULL
2	Office	           NULL           NULL
3	Keyboard	   2            Office
Help with a query pleasssse?
Maybe it is easier if I create extra field and not only store ChildOf but also ChildParentName during inserting/edit etc

Last edited by internationalist; 02-07-08 at 10:38.
Reply With Quote
  #6 (permalink)  
Old 02-07-08, 11:33
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
so adjust the query to include the column(s) you want.
Reply With Quote
  #7 (permalink)  
Old 02-11-08, 07:36
Marvels Marvels is offline
Registered User
 
Join Date: Jul 2003
Location: Amsterdam, Nederland
Posts: 449
What Healdem said.

Code:
Select A.ID,A.Parent_name,A.ChildOf  , A.Parent_name AS [PARENT of CHILD]from testen as A left join testen as B on B.childof = A.ID;
__________________
Greetz Marvels -^.^-
Developments : VB4 Through .Net; Basic; DOS ; CNC ; Sinclair
Databases : SQL Server Through 2005; Access 3 Through 2003 ; Oracle 8 & 9.i ;
OS : Win 3.11 Through XP ; NortonComander ; DOS
Reply With Quote
  #8 (permalink)  
Old 02-14-08, 18:32
internationalist internationalist is offline
Registered User
 
Join Date: Jan 2008
Posts: 17
Code:
ID	Parent_name	ChildOf     ChildParentName
------------------------------------------------------
1	PC	           NULL           PC
2	Office	           NULL           Office
3	Keyboard	   2            Keyboard
That is the result, which I dont want.

but this did it
Code:
Select a.id,a.parent_name,a.childof , b.parent_name AS parent_name from testen as a left testen as b on b.id = a.childof;
Thank you!

Last edited by internationalist; 02-14-08 at 18:37.
Reply With Quote
  #9 (permalink)  
Old 02-16-08, 07:10
JAILO0330 JAILO0330 is offline
Registered User
 
Join Date: Feb 2008
Posts: 2
Please try this query:

SELECT
id
,parent_name
FROM tbl_resource
WHERE childof IS NOT NULL

Last edited by JAILO0330; 02-16-08 at 07:13. Reason: Error on typing
Reply With Quote
  #10 (permalink)  
Old 02-16-08, 07:21
JAILO0330 JAILO0330 is offline
Registered User
 
Join Date: Feb 2008
Posts: 2
Sorry I wasnt able get the idea from the readof the problem:

I think this will do the trick:

SELECT
a.id
,a.Parent_name
,a.ChildOf
,'ChildParentName' = b.Parent_name
FROM tbl_resource a
LEFT JOIN tbl_resource b
ON a.ChildOf = b.ID

Last edited by JAILO0330; 02-16-08 at 07:22. Reason: Typing Error
Reply With Quote
  #11 (permalink)  
Old 02-16-08, 09:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
jailo, you are allowed to say 'ChildParentName' = b.Parent_name in the SELECT list in mysql, but the results are not what you think they are

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On