dbTalk Databases Forums  

help on SQL statement

comp.databases comp.databases


Discuss help on SQL statement in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default help on SQL statement - 06-08-2006 , 02:21 PM






Hi,

I have two tables: the first one has all details about persons, the
second associate the id of each person to the id of his team leader
(id_boss). Bosses themselves are present in the first table

DETAILS_TBL
id_person
first_name
name

PERSON_IN_GROUP_TBL
id_person
id_boss

I must make a query to have this result

[employee] - [boss]
john - smith - jack - doe
michael - ross - jack - doe
jack - doe - jack - doe
tom - cruise - al - pacino

etc

I use mysql and postgresql but I can't imagine a sql statement to do
this.

Anyone can help me?
:-)

BR,

Filippo


Reply With Quote
  #2  
Old   
David Cressey
 
Posts: n/a

Default Re: help on SQL statement - 06-08-2006 , 04:07 PM







<filippo2991 (AT) virgilio (DOT) it> wrote

Quote:
Hi,

I have two tables: the first one has all details about persons, the
second associate the id of each person to the id of his team leader
(id_boss). Bosses themselves are present in the first table

DETAILS_TBL
id_person
first_name
name

PERSON_IN_GROUP_TBL
id_person
id_boss

I must make a query to have this result

[employee] - [boss]
john - smith - jack - doe
michael - ross - jack - doe
jack - doe - jack - doe
tom - cruise - al - pacino

etc

I use mysql and postgresql but I can't imagine a sql statement to do
this.

Anyone can help me?
:-)

BR,

Filippo

This is very simple SQL. Almost beginner level.
You need three source tables... The person_in_group_tbl once, and the
details_tbl twice. In order to be able to talk about columns, you have to
use different table aliases for the two instances of the details table.



The answer is something like this:

select
emp.first_name as employee_first,
emp.name as employee_name,
boss.first_name as boss_first,
boss.name as boss_name
from
details_tbl as emp,
person_in_group_tbl as p,
details_tbl as boss
where
emp.id_person = p.id_person and
boss.id_person = p.id_boss

You may need to clean up the column headers, but you get the idea.











Reply With Quote
  #3  
Old   
filippo
 
Posts: n/a

Default Re: help on SQL statement - 06-08-2006 , 04:36 PM




David Cressey ha scritto:

Quote:
You may need to clean up the column headers, but you get the idea.
fine, I have to digest this :-).
My problem is that I have to implement a search query like

SELECT

(ALL THE QUERY YOU SHOWN)

WHERE

details_tbl.first_name = 'john'

How can I do?

Can I do like this?

SELECT
details_tbl.name
details_tbl.born
FROM
(YOUR PREVIOUS QUERY)
WHERE
details_tbl.first_name = 'john'



Reply With Quote
  #4  
Old   
Lennart
 
Posts: n/a

Default Re: help on SQL statement - 06-12-2006 , 04:24 PM




filippo wrote:
Quote:
David Cressey ha scritto:

You may need to clean up the column headers, but you get the idea.

fine, I have to digest this :-).
My problem is that I have to implement a search query like

SELECT

(ALL THE QUERY YOU SHOWN)

WHERE

details_tbl.first_name = 'john'

How can I do?
What will happen if you add your predicate (details_tbl.first_name =
'john') to the where clause of Davids query?

[...]

/Lennart



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.