I have problems with INNER JOIN SQL SERVER

2

I'm new in what data base refers .. and I'm working with the issue of INNER JOIN and the truth is that I'm not very clear about how it works and how to apply .. since I have an exercise and I can not join a third table that is related .. someone would be so kind to explain it to me .. here is an example of what I am doing .. I do not know if it is right or wrong ... since it does not give me any results ..

select 
  TDetalle_pedido.id_detalle_pedido,
  TProducto.nombre_producto, 
  TDetalle_pedido.cantidad,
  TCliente.nombre_cliente 
from TDetalle_pedido
inner join TProducto on TDetalle_pedido.id_producto = TProducto.id_producto
inner join TPedido on TDetalle_pedido.id_pedido = TPedido.id_pedido 
inner join TCliente on TDetalle_pedido.id_pedido = TCliente.id_cliente 
order by TProducto.nombre_producto;

    
asked by Ernesto Schwan 10.12.2018 в 21:08
source

1 answer

3

The problem seems to be in this line:

inner join TCliente on TDetalle_pedido.id_pedido = TCliente.id_cliente 

You are doing join between id_pedido e id_cliente , which I imagine is incorrect.

Following your data model, the join should be:

select 
  dp.id_detalle_pedido,
  pr.nombre_producto, 
  dp.cantidad,
  cl.nombre_cliente 
from TDetalle_pedido dp
inner join TProducto pr
    on dp.id_producto = pr.id_producto
inner join TPedido pe
    on dp.id_pedido = pe.id_pedido 
inner join TCliente cl
    on pe.id_cliente = cl.id_cliente 
order by p.nombre_producto;
    
answered by 10.12.2018 в 21:20