Re: Hint for a query

Поиск
Список
Период
Сортировка
От Uwe Schroeder
Тема Re: Hint for a query
Дата
Msg-id 201111032352.40630.uwe@oss4u.com
обсуждение исходный текст
Ответ на Hint for a query  (Efrain Lopez <tecnomaya@cabsagt.com>)
Список pgsql-general

> I have this tables
>
>
> Table: Contact
> IdContact
> First Name
> Second Name
> … other columns
>
> Table: Employee
> IdEmployee
> IdContact, related to Contact table
> … other columns
>
> Table: Salesman
> IdSaleman
> IdEmployee, if salesman is employee, related to Employee table
> IdContact, if salesman is not an employee, related to Contact table
>
>
> I need a query
>
> Id Salesman - Second name - First name
>
> But I can't figure how to do it, can someone can give advise?
>
> Thanks


Needless to say, this is bit of an odd table layout. You always end up at the
contact table, but the layout makes it harder to query. Personally I'd have a
foreign key from the contact table to the employee table, rather than the
employee table to the contact table - that would also eliminate the employee
foreign key in the salesman table. It would also allow you to just join the
salesman table to the contact table and then figure out if the contact is an
employee.

well, that said. Here's a quick one without a lot of deep thought...

select a.idsalesman, b.firstname, b.secondname from salesman a join contact b
on b.idcontact=a.idcontact union  select c.idsalesman, d.firstname,
d.secondname from salesman c join employee e on e.idemployee=c.idemployee join
contact d on d.idcontact=e.idcontact

No guarantees though. It's midnight here and I had a long day...

В списке pgsql-general по дате отправления:

Предыдущее
От: Thomas Mieslinger
Дата:
Сообщение: Re: Recommendations for SSDs in production?
Следующее
От: Naoko Reeves
Дата:
Сообщение: Re: ERROR from pg_restore - From OS X to Ubuntu