List an item accompaniments from a mysql sale

Asked

Viewed 84 times

2

Table 1 - sale
id_venda

Table 2 - item_request
id | id_venda | id_pastel

Table 3 - item_accompaniment
id | id_item_request | id_follow-up

Example: I make a sale with two items and their accompaniments:

A pastel:
meat (item requested)
Accompaniments(item_accompaniment)
onion and parsley.

A pastel:
chicken(item_request)
Accompaniment:
catupiry(item_accompaniment).

Separately I can even bring:

SELECT venda.id_venda, 
       GROUP_CONCAT(item_pedido.id_pastel) AS id_pastel 
FROM venda 
JOIN item_pedido ON item_pedido.id_venda = venda.id_venda GROUP BY item_pedido.id_venda

inserir a descrição da imagem aqui

SELECT item_pedido.id_venda, item_pedido.id_pastel,
GROUP_CONCAT(item_acompanhamento.id_acompanhamento) AS id_acompanhamento 
FROM item_pedido 
JOIN item_acompanhamento ON item_pedido.id = item_acompanhamento.id_item_pedido 
GROUP BY item_pedido.id

inserir a descrição da imagem aqui

But I would like to bring only one sale with the two items with their respective side dishes.

  • would like to bring only one sale with the two items with their respective side dishes and that’s not exactly what this query result is showing?

1 answer

2

Utilizes the distinct in the SELECT what you do. I think it presents the set or the last record inserted with the id_sale.

SELECT DISTINCT item_pedido.id_venda, item_pedido.id_pastel,
GROUP_CONCAT(item_acompanhamento.id_acompanhamento) AS id_acompanhamento 
FROM item_pedido 
JOIN item_acompanhamento ON item_pedido.id = item_acompanhamento.id_item_pedido 
GROUP BY item_pedido.id

If it doesn’t work, the idea is to create an auxiliary table that combines common sales.

  • I appreciate the answer but with DISTINCT it didn’t work. I didn’t understand the second option about creating an auxiliary table to join common sales.

  • I’ve been thinking about the solution, and I just don’t understand why you put the sale together... you want a line that says 11.95 - 2.5.6 ? so you can’t tell sales apart...

  • I wanted something like 11.2 and 95.5.6, where 11 is the sale and 2 is the accompaniment.

  • If it works, it was to concatenate the sale and the accompaniment with a comma in the middle... Views would probably help, I mean... If you are showing in PHP you can do it quietly.

  • 1

    Get it done the way it is right there. As the first query returns the two id’s, I entered them and with another select I got the result I wanted. I appreciate your help.

Browser other questions tagged

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