Help with Join in Mysql [duplicate]

0

I found several questions / answers about join on the site, but I still can not create the command I need. What I need is the following:

I have the following tables:

item (I have two items registered)

id
descricao
---------------
1 - dipirona
2 - paracetamol

item_details (of these items, I have only dipirone of two different brands)

id
item
marca
-------------------
1 - 1 - medley
2 - 1 - medlab

current item (in stock, I have 1000 brands of one brand and 1500 of another)

id
itemdetalhe
qtd
----------------
1 - 1 - 1000
2 - 2 - 1500

What I need is to generate something like this:

item.id | itemdetalhe.id | item.descricao | itemdetalhe.marca | itematual.qtd
1         1                dipirona         medley              1000
1         2                dipirona         medlab              1500
2                          paracetamol 
    
asked by anonymous 06.08.2018 / 13:35

1 answer

1

In case you need to make a JOIN that brings the information from the base table even if there is no data in the reference tables. In this other answer you can check the differences between JOINS . Applying on your need:

SELECT i.id AS 'item_id',
      id.id AS 'item_detalhe_id',
      i.descricao,
      id.marca,
      ia.qtd
  FROM item i
      LEFT JOIN itemdetalhe id ON id.item = i.id
      LEFT JOIN itematual ia ON ia.itemdetalhe = id.id

Where item data will appear even if there is no data in the itemdetalhe and itematual tables.

    
06.08.2018 / 14:03