SQL Query-Repeated Data

0

Good! I have two tables ( VOIPCLIENTE , VOIPMAQUINA ) where in VOIPCLIENTE I have information about the clients ( CLIENTE,DELEGACIÓN... ) and in VOIMPAQUINA on the machines ( MAQUINA,EMPRESA... ).

These two tables have more fields but only one field relates them, CLIENTE=EMPRESA .

When I do my sql query, relating those two fields and consulting the MAQUINA I get repeated machines.

The query:

SELECT maquina.MAQUINADELEG,clientes.CLIENTE,maquina.EMPRESA,maquina.MAQUINA
FROM dbo.VOIPCLIENTE clientes, dbo.VOIPMAQUINA maquina
WHERE maquina.EMPRESA = clientes.CLIENTE order by maquina

The result:

As you can see, 3 repeated rows always come out.

    
asked by talendguy 29.11.2016 в 09:45
source

3 answers

3

If the primary customer key is CLIENTEDELEG , it is incorrect to refer to via clientes.CLIENTE . You should verify that the reference maquina.EMPRESA really (that is, according to design, and according to Foreign Key constraint, if it is being used) points to clientes.CLIENTE and, by the way, verify that clientes.CLIENTE is, if not a primary key, at least one key secondary or a field with uniqueness restriction.

A quick inquiry to investigate:

SELECT * from cliente where CLIENTE = 100004

If that throws several results, then we are in trouble either design or interpretation of the design (step: a design that uses the same name for the table and a column of that table ... does not smell well )

    
answered by 29.11.2016 / 13:37
source
0

You can do a SELECT DISTINCT with the Machine and then a JOIN with the rest of the elements. SELECT DISTINCT

Simply try this:% SELECT DISTINCT maquina.MAQUINADELEG,clientes.CLIENTE,maquina.EMPRESA,maquina.MAQUINA FROM dbo.VOIPCLIENTE clientes, dbo.VOIPMAQUINA maquina WHERE maquina.EMPRESA = clientes.CLIENTE order by maquina

    
answered by 29.11.2016 в 11:35
0

Two things you should first if you want to relate to use an inner join and then put the relationship machine. EMPPRESS = customers.CLIENT, taking into account that this is the relationship which I doubt is to say

SELECT maquina.MAQUINADELEG,clientes.CLIENTE,maquina.EMPRESA,maquina.MAQUINA
FROM dbo.VOIPCLIENTE clientes
INNER JOIN dbo.VOIPMAQUINA maquina ON maquina.EMPRESA = clientes.CLIENTE 
ORDER by maquina asc

second if you only want to use your query and not being as efficient use a distinct to eliminate repeated values

SELECT maquina.MAQUINADELEG,clientes.CLIENTE,maquina.EMPRESA,maquina.MAQUINA
FROM dbo.VOIPCLIENTE clientes, dbo.VOIPMAQUINA maquina
WHERE maquina.EMPRESA = clientes.CLIENTE order by maquina
    
answered by 20.10.2018 в 17:51