Query between two sqlite tables

Asked

Viewed 1,210 times

1

My question is how to make a query between two tables.

Would be a table Pedidos with the countryside Idprod, idCliente.

The other would be the table of Produtos with the fields Idprod, Descricao.

I wanted to do a survey of Produtos purchased by the particular customer.

I’m using visual studio 2015 Xamarin Forms, database sqlite.

I’ve tried that but it didn’t work:

Select Idprod,Descricao From Produtos INNER JOIN pedidos on Produtos.Idprod = pedidos.Idprod 
  • 3

    Your structure has only one product per order? When you say it did not work, what does it mean? It was wrong? It would be interesting to ask the question examples of data and results you got and result you hoped to get.

2 answers

1

I don’t know how your business rule is defined, but when I look at it superficially I think it’s incorrect.

The relationship of pedido is of 1...N, you should have an entity pedido_produto where the relationship between orders and products is made.

requested table

+----+------------+
| id | cliente_id |
+----+------------+
|  1 |          2 |
|  2 |          3 |
|  3 |          1 |
+----+------------+

table_product

+----+------------+
| id | descricao  |
+----+------------+
|  1 | SMART TV   |
|  2 | NOTEBOOK   |
|  3 | SMARTPHONE |
+----+------------+

table_pedido_product

+-----------+------------+
| pedido_id | produto_id |
+-----------+------------+
|         1 |          2 |
|         1 |          3 |
|         2 |          1 |
|         2 |          3 |
|         3 |          1 |
+-----------+------------+

With the following DDL:

CREATE TABLE tabela_cliente (
    id INT AUTO_INCREMENT PRIMARY key,
    nome VARCHAR(100)
);

CREATE TABLE tabela_pedido (
    id INT AUTO_INCREMENT PRIMARY key,
    cliente_id INT
);

CREATE TABLE tabela_produto (
    id INT AUTO_INCREMENT PRIMARY key,
    descricao VARCHAR(100)
);

CREATE TABLE tabela_pedido_produto (
    pedido_id INT,
    produto_id INT,
);

INSERT INTO tabela_cliente (nome) VALUES
('João'),
('José'),
('Maria'),
('Madalena');

INSERT INTO tabela_pedido (cliente_id) VALUES
(1),
(2),
(3),
(4);

INSERT INTO tabela_produto (descricao) VALUES
('SMARTPHONE'),
('SMARTV'),
('NOTEBOOK');


INSERT INTO tabela_pedido_produto (pedido_id, produto_id) VALUES
(1, 1),
(1, 2),
(2, 2),
(2, 3),
(3, 1),
(4, 2);

You would select orders by listing customers and products:

SELECT * 
FROM tabela_pedido p
LEFT JOIN tabela_cliente c ON c.id = p.cliente_id
LEFT JOIN tabela_pedido_produto pp ON pp.pedido_id = p.id
LEFT JOIN tabela_produto pr ON pr.id = pp.produto_id

If for example, you want to get customer’s orders and products John, just add the clause WHERE:

WHERE p.cliente_id = 1

0

Hi @Eloi,

Try it this way:

Select Produtos.Idprod
     , Produtos.Descricao 
  From Produtos INNER JOIN Pedidos on Produtos.Idprod = Pedidos.Idprod
 Where Pedidos.IdCliente = 'id do cliente'

The way you wrote it will give ambiguity error, both the table Produtos as to table Pedidos have the column Idprod, then you need to make explicit in select which table belongs to the column, Produtos.Idprod or Pedidos.Idprod.

[]s


Eloi I need these details in the red square to understand what happens, the more information you provide about the error the better:

inserir a descrição da imagem aqui

  • Felipe, I did as you suggested. I did in Sqlite Explorer ai worked well but when I try my project does not work. I am using the visual studio 2015, cross-platform application (Xamarin Forms) my code is in a class: public List<Products> Listaprod(Orders orders) Query<Products> (" Select Products.Idprod , Products.Description From Products INNER JOIN Orders on Products.Idprod = Orders.Idprod"); }

  • Got it...in this case I need you to explain to me what happens, throw an error? Returns an empty list?

  • yes throws an error, opens a message " Na unhandled Exception occured. " as option to interrupt or continue, when I choose to continue the application terminates.

  • I put an example of what I need about the errors in the answer

Browser other questions tagged

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