I can’t perform a Select

Asked

Viewed 83 times

3

I’m having a question about how I should ride the SELECT for a particular function.

The scenario is as follows:

I have 4 tables: Tab_Pessoa - Tab_Cliente - Tab_Autorizado - Tab_Cliente_Autorizado. So that the Tab_Pessoa relates to that of Tab_Cliente and Tab_Autorizado - To Tab_Cliente_Autorizado relates to that of Tab_Cliente and Tab_Autorizado. Succinctly Tab_Pessoa has the name field.

I want to get the following information: The name of the authorised person of a particular customer.

Otherwise to be clearer:

In the Tab_Pessoa has the following records:

cod_pessoa  | Nome
1           | Luis
2           | Carlos
3           | Paulo

In the Tab_Cliente has the following records:

cod_cliente | cod_pessoa 
1           | 1

In the Tab_Autorizado has the following records:

cod_autorizado  | cod_pessoa 
1               | 2
2               | 3

In the Tab_Cliente_Autorizado has the following records:

cod_cliente_autorizado | cod_cliente | cod_autorizado 
1                      | 1           | 1
2                      | 1           | 2

I want you with cod_cliente = 1 I get the name of his authorized officer: Carlos and Paulo

I’ve assembled several SELECT's but I did not succeed, in my mind initially it would be that SELECT would that way:

Select pes.nome from tab_Cliente_Autorizado cli_aut 
inner join tab_Cliente cli on cli.cod_cliente = cli_aut.cod_cliente
inner join tab_Pessoa pes on cli.cod_pessoa = pes.cod_pessoa
where cli_aut.cod_cliente = 1

But what I get is the client’s name twice.

I am grateful for any suggestion.

Update - With tables script

--Table Person

go
create table tab_Pessoa(
cod_pessoa int identity(1,1) not null,
nome nvarchar(200) null,
constraint pk_pessoa Primary Key (cod_pessoa))
go

--Table Client

go
create table tab_Cliente(
cod_cliente int identity(1,1) not null,
cod_pessoa int not null,
constraint pk_cliente Primary Key (cod_cliente),
constraint fk_tab_Cliente_tab_Pessoa foreign key(cod_pessoa) references tab_Pessoa(cod_pessoa))

--Authorized Table

go
create table tab_Autorizado(
cod_autorizado int identity(1,1) not null,
cod_pessoa int not null,
constraint pk_autorizado Primary Key (cod_autorizado),
constraint fk_tab_Autorizado_tab_Pessoa foreign key(cod_pessoa) references tab_Pessoa(cod_pessoa))
go

--Client Table - Authorized

go
create table tab_Cliente_Autorizado(
cod_cliente_autorizado int identity(1,1) not null,
cod_cliente int not null,
cod_autorizado int not null,
constraint pk_cliente_autorizado Primary Key (cod_cliente_autorizado),
constraint fk_tab_Cliente_Autorizado_tab_Autorizado foreign key(cod_autorizado) references tab_Autorizado(cod_autorizado),
constraint fk_tab_Cliente_Autorizado_tab_Cliente foreign key(cod_cliente) references tab_Cliente(cod_cliente))
go
  • Old man, can you pull up the table script? It would help a lot, I think the problem is in the relationship... if you see need, create your example of what is happening there: http://sqlfiddle.com/

  • See details of how to edit here: http://answall.com/editing-help

1 answer

4


You don’t need to involve the table Tab_client in SELECT because you will not fetch any data from it.

You need to get the authorization codes from a customer in the table Tab_cliente_authorized and based on these codes seek the authorized persons of this client in the table Tab_authorized, and based on the codes of the people found you need to get the names of the people in Tab_pessoa.

Then the relationship could be represented like this: Authorized client -> Authorized user -> Tab_person -> Tab_person. SELECT is like this:

select 
    Tab_Pessoa.Nome
from
    Tab_Cliente_Autorizado
    JOIN Tab_Autorizado on Tab_Autorizado.cod_autorizado = Tab_Cliente_Autorizado.cod_autorizado
    JOIN Tab_Pessoa on Tab_Pessoa.cod_pessoa  = Tab_Autorizado.cod_pessoa
where
    Tab_Cliente_Autorizado.cod_cliente = 1

Note: If your tables only contain this data, you don’t need the table Tab_authorized. Instead, it could relate the person authorized directly on Tab_cliente_authorized. This table would look like this:

cod_cliente_autorizado | cod_cliente | cod_pessoa 
1                      | 1           | 2
2                      | 1           | 3

Another note: you don’t need the prefix "Tab_" in table names.

  • That’s right, thank you very much. I had no knowledge that I could only use Join.

  • Now that I’ve looked more closely, I’ve seen the complexity of the select you’ve made. Congratulations would never think about it.

  • @Luisfelipemicaidejesus Of Nothing. See my update on the answer with a remark on your modeling. As to using only the word JOIN, this is a link to the INNER JOIN. That is, if the type and direction of JOIN (inner, or Outer left or Outer right) are omitted, the default is assumed: Inner Join.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.