Doubt with select in mysql

0

Well I have the following tables:

Orders

id      id_cliente      id_vendador
1       10              20
2       10              30
3       10              20

Devolutions

Id      data            id_pedido
1       2017/01/01      3

I need to do a 'select' by entering the 'id' of the requests table, and it has to return me the returns that contain the same clients as the requests.

Example:

and id IN(1,2)

It has to return the ID 1 of the return because the 'order_id' of it is 3 and the 'customer_id' is the same as the request.

Can anyone help me? I've already broken my head here.

    
asked by anonymous 04.02.2017 / 02:25

1 answer

1

You only need to INNER JOIN in the same table by passing the field you want to bind, in this case id_cliente :

SELECT dev.id
  FROM pedidos ped
 INNER JOIN pedidos ped2 ON ped2.id_cliente = pd.id_cliente
 INNER JOIN devolucoes dev ON dev.id_pedido = ped2.id
 WHERE ped.id = [INSIRA AQUI O ID DO PEDIDO]
    
04.02.2017 / 02:55