0
To table A is the table that receives several rows containing different values related to table B which is where the items are stored.
Table A
Descrição | Valor
item 1 | 50,00
item 1 | 40,00
item 2 | 10,00
Table B
Descrição | Valor
item 1 | 100,00
item 2 | 100,00
I’m trying to get the statement to select the descriptions from table A whose values are less than table B. The fact that I am grouping is because there are lines with the same description dealing with the same item, but with different values.
SELECT tabela_A.descricao
FROM tabela_A
INNER JOIN tabela_B
WHERE tabela_A.id = tabela_B.id
AND (SELECT SUM(tabela_A.valor)
FROM tabela_A
INNER JOIN tabela_B
WHERE tabela_A.id = tabela_B.id
GROUP BY tabela_B.descricao) < tabela_B.valor
GROUP BY tabela_B.descricao
So it would be more practical, but it is wrong. But it can help to better understand the point I want to reach:
SELECT tabela_A.descricao, SUM(tabela_A.valor) AS _valor
FROM tabela_A
INNER JOIN tabela_B
WHERE tabela_A.id = tabela_B.id
AND _valor < tabela_B.valor
GROUP BY tabela_B.descricao
Aaah yes... I forgot about this detail. When comparing table A with table A was a lack of attention when posting. I used a fictional example to better understand, but it’s correct in my code. The truth is that I’m thinking that doing everything with SQL will not work, I’ll have to appeal to conditions via PHP. I appreciate your help right now!
– Eduardo AC