I need to list the amount of an 'X' product that each customer bought

Asked

Viewed 3,313 times

2

Below follows the structure and relationships of my database. estrutura e relacionamentos do banco de dados Nota.

I tried to use the code :

`SELECT `cliente`.`Nome`, `produto`.`Descricao`, 
 `contem`.`QuantItem`
 FROM `cliente`, `produto`, `contem`
 INNER JOIN `pedido`
 ON `cliente`.`Codigo` = `pedido`.`Codigo`
 INNER JOIN `contem`
 ON `contem`.`numero` = `pedido`.`Numero`
 WHERE `contem`.`Codigo` = 1;`

but it presents the following error when executing: #1066 - Table/alias contains not only.

I would like to know how I can correct and/or what I must do to show the desired result.

I thank you in advance for your cooperation.

  • @Caiqueromero but if he wants to get content from the other two tables (product.Description, contains.Quantitem) how he will look only at one table?

  • @R.Santos unfortunately correcting this, still continues the same error.

  • @Caiqueromero, cara I tried what you suggested, but presented the following error: #1054 - Product column. Unknown 'description' in 'field list', I believe it must be necessary that they remain in the declaration.

  • I wrote wrong take only the contents that are in the FROM, let me know if it works

2 answers

2


What you want can be achieved by summing the grouping according to the customer:

SELECT cli.Nome AS cliente,
       prod.Descricao AS produto,
       SUM(co.QuantItem) AS quantidade
  FROM cliente cli
       INNER JOIN pedido p ON p.Codigo = cli.Codigo
       INNER JOIN contem co ON co.Numero = p.Numero
       INNER JOIN produto prod ON prod.Codigo = co.Codigo
 WHERE co.Codigo = 1
 GROUP BY cli.Nome,
          prod.Descricao
  • 1

    perfect. This code presented the expected result. Thank you very much.

0

Table contains appears twice in your query, remove it from from

SELECT cliente.Nome
  , produto.Descricao 
  , contem.QuantItem
FROM cliente
INNER JOIN pedido
   ON cliente.Codigo = pedido.Codigo
INNER JOIN contem
   ON contem.Numero = pedido.Numero
INNER JOIN produto
   ON produto.Codigo = contem.Codigo
WHERE contem.Codigo = 1;
  • of this error message. #1054 - Unknown client column.Code in 'on clause'

Browser other questions tagged

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