problems with distinct in mysql

Asked

Viewed 308 times

3

I have a select where I have to sort it by the field 'Qtd' the problem is that I have this different field, and I need the products not to repeat.

The 'a. id' field stores the product code, that is, you cannot repeat. Someone knows how to fix this?

The field 'b. Qtd' has to be the sum of the two UNION tables.

Follow my select:

SELECT 
        DISTINCT a.id, 
        a.unidade, 
        a.posicao, 
        a.nome, 
        a.peso, 
        sum(b.qtd) quant 
    FROM 
        produtos a, 
        produtos_pedidos b 
    WHERE 
        a.id = b.id_produto 
        and b.id_pedido IN (3,2) 
    GROUP BY 
        a.id, 
        a.unidade, 
        a.posicao, 
        a.nome, 
        a.peso 

    UNION

    SELECT 
        DISTINCT c.id, 
        c.unidade, 
        c.posicao, 
        c.nome, 
        c.peso, 
        sum(d.qtd) quant 
    FROM 
        produtos c, 
        pedidos_barganha d 
    WHERE 
        c.id = d.id_produto 
        and d.id_pedido IN (3,2) 
    GROUP BY 
        c.id, 
        c.unidade, 
        c.posicao, 
        c.nome, 
        c.peso
    ORDER BY quant DESC
  • I don’t understand your question. You what to do distinct just by the id? Is that it? If yes, you can do select distinct (a.id), a.unidade, a.posicao....

  • That’s right, 'a. id' and 'c.id' cannot repeat themselves, and 'Quant' has to be the total of 'Qtd'. I tried it your way and keep repeating

3 answers

5

Making a JOIN between the produtos and a union of produtos_pedidos should satisfy it. Something like that:

SELECT
  a.id,
  a.unidade,
  a.posicao,
  a.nome,
  a.peso,
  sum(d.qtd) quant
FROM
 produtos a JOIN
(
 SELECT b.id_produto, b.qtd
 FROM produtos_pedidos b
 WHERE b.id_pedido IN (3,2)

 UNION ALL

 SELECT c.id_produto, c.qtd
 FROM pedidos_barganha c 
 WHERE c.id_pedido IN (3,2)
) d ON a.id = d.id_produto
GROUP BY  
  a.id,
  a.unidade,
  a.posicao,
  a.nome,
  a.peso
ORDER BY quant DESC
  • good is giving this error '#1054 - Column 'b. id_product' unknown in 'field list''

  • added the b in the product ordering table. It is a simple alias. = D

  • now gave this error here '1140 - In aggregated query without GROUP BY, Expression #1 of SELECT list contains nonaggregated column '0aaa9006a38a4698ada2dde6bd2a3a.a. id'; this is incompatible with sql_mode=only_full_group_by'

  • put the GROUP BY that was missing.

  • worked out, thank you very much

  • I noticed a problem, the products with the same Qtd do not appear and are not summed in sum, have to fix it?

  • Yes, use the UNION ALL instead of the UNION. edited the answer.

  • OK gave it right, thank you very much

Show 3 more comments

5


Your problem is that you have 2 tables with different information and want to add them. Your solution may work, but it will take a little time.

As an alternative solution I can suggest the use of a temporary table, which can be much simpler to use, while keeping the code well readable.

CREATE TEMPORARY TABLE resultados (
  id_produto INT NOT NULL,
  qtd INT NOT NULL
);

INSERT INTO resultados(id_produto, qtd)
SELECT
  A.id,
  SUM(B.qtd)
FROM produtos AS A
INNER JOIN produtos_pedidos AS B
  ON (A.id = B.id_produto)
WHERE B.id_pedido IN (3,2) 
GROUP BY A.id;

INSERT INTO resultados(id_produto, qtd)
SELECT
  A.id,
  SUM(B.qtd)
FROM produtos AS A
INNER JOIN pedidos_barganha AS B
  ON (A.id = B.id_produto)
WHERE B.id_pedido IN (3,2) 
GROUP BY A.id;

/* ADICIONA UM INDICE PARA MELHORAR A CONSULTA */
ALTER TABLE resultados ADD KEY `indice_temporario_01` (id_produto);

SELECT
  A.id_produto,
  SUM(A.qtd) AS QUANTIDADE,
  B.unidade, 
  B.posicao, 
  B.nome, 
  B.peso
FROM resultados AS A
INNER JOIN produtos AS B
  ON (A.id_produto = B.id)
GROUP BY A.id_produto
ORDER BY QUANTIDADE;

Observing: I am considering that both the product and its quantity are whole. Change the table creation to the correct data types if necessary.

This query will bring the total amount of tables produtos_pedido and pedidos_barganha.

An important detail: I understand that the column id table produtos is a unique key. For this reason, there is no need to put the other columns in the clause GROUP BY.

One last performance tip: If possible, you can still change the creation of the temporary table to use a faster engine like the MYISAM instead of the standard InnoDb.

  • Well, the quantity will not always be whole, because there are products that are sold in KG. I tried to do it your way, but I have this error '#1054 - Unknown 'A.id' column in 'field list''

  • There was an error in the last select. I made a small adjustment to correctly catch the column name. If you can make CREATE TABLE available, it would be better, because we can build more collections.

  • ok now it worked out, and as I order it by field quantity?

  • 1

    You can add an ORDER BY in the last query. I will edit the reply and add the sort

  • very good :) thank you

2

So, buddy, did you try to do it with Seamanship? Something in this sense:

SELECT DISTINCT a.id, 
    a.unidade, 
    a.posicao, 
    a.nome, 
    a.peso, 
    sum(b.qtd) quant
FROM ( 
    (SELECT 
        a.id, 
        a.unidade, 
        a.posicao, 
        a.nome, 
        a.peso, 
        sum(b.qtd) quant 
    FROM 
        produtos a, 
        produtos_pedidos b 
    WHERE 
        a.id = b.id_produto 
        and b.id_pedido IN (3,2) 
    GROUP BY 
        a.id, 
        a.unidade, 
        a.posicao, 
        a.nome, 
        a.peso

    UNION

    SELECT 
        c.id, 
        c.unidade, 
        c.posicao, 
        c.nome, 
        c.peso, 
        sum(d.qtd) quant 
    FROM 
        produtos c, 
        pedidos_barganha d 
    WHERE 
        c.id = d.id_produto 
        and d.id_pedido IN (3,2) 
    GROUP BY 
        c.id, 
        c.unidade, 
        c.posicao, 
        c.nome, 
        c.peso
    ORDER BY quant DESC) as tbl
)

This way you would apply DISTINCT in the final results and not in each individual table.

UPDATE: I made a change to the code, see if it works, I entered the aliases for each table up there

UPDATE 2: New change in code, reduced to a virtual table only. Try to check now

  • I get it, I tried it here and it’s giving this error. #1248 - Each derived table must have its own alias

  • well the error now and this one '#1064 - You have a syntax error in your SQL next to 'as tbl2 )' at line 49'

Browser other questions tagged

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