SQL Inner Join 3 tables

Asked

Viewed 750 times

-1

Good afternoon. I want to make an Internet but the result does not come out. inserir a descrição da imagem aqui

Tabela1 = rec  Campo = chvbfj(3)      <- codigo do cliente
Tabela1 = rec  Campo = doc(3,4,5)     <- lista de codigos de itens do cliente
Tabela2 = vndB Campo = chvvnda(3,4,5) <- tem o codigo de itens do cliente  
Tabela2 = vndB Campo = chvps(1,1,2)   <- codigo da descrição  
Tabela3 = ps   Campo = chvps(1,1,2)   <- tem o codigo da descrição  
Tabela3 = ps   Campo = Desc(escova, escola, lixa) <- descricao do codigo

Making the client’s query(3) he lists the items(1,1,2), but would have to appear the code description(brush, brush, sandpaper).

 public DataTable PesquisaResumo(global::CamadaModelos.mdlEmpresa _mdlEmpresa)
    {
        string ConexaoAccess = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\tabela.mdb";
        OleDbConnection ConexaoDB = new OleDbConnection(ConexaoAccess);
        ConexaoDB.Open();

        string Query = "SELECT rec.chvbfj, rec.doc, vndB.chvvnda, vndB.chvps, ps.chvps, ps.Dsc FROM rec INNER JOIN vndB on vndB.chvvnda = rec.doc INNER JOIN ps on ps.chvps = vndB.chvps WHERE rec.chvbfj = @id";

        OleDbCommand cmd = new OleDbCommand(Query, ConexaoDB);

        cmd.CommandType = CommandType.Text;
        OleDbParameter pmtID = cmd.CreateParameter();
        pmtID.ParameterName = "@id";
        pmtID.DbType = DbType.String;
        pmtID.Value = _mdlEmpresa.ID;
        cmd.Parameters.Add(pmtID); 

        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataTable empresas = new DataTable();
        da.Fill(empresas);
        ConexaoDB.Close();
        return empresas;
    }
  • How is your current query?

  • select chvbfj, doc from rec Where chvbfj=@id

  • This link may be useful to you: https://www.devmedia.com.br/utilizando-joins-em-sql/1071

  • in your sql TA THUS vndB.chvps, ps.chvps, can not have two repeated names strip 1 of them

  • except either one or both of the error. (operator missing)

  • then I’ll change the SQL pera ae

  • try that one: SELECT rec.chvbfj, rec.doc, vndB.chvvnda, vndB.chvps, ps.Desc FROM vndB &#xA; INNER JOIN rec ON vndB.chvvnda = rec.doc&#xA; INNER JOIN ps ON vndB.chvps = ps.chvps&#xA;WHERE rec.chvbfj = 3

  • 'Syntax error (missing operator) in query expression 'vndB.chvvnda = rec.doc INNER JOIN ps ON vndB.chvps = ps.chvps'.'

  • Next Thiago the error is in the tables, there is no way we test the way to do it is so expensive has no where, this can be mixed data, this can be so much, it is a local problem.

  • It looks like you are using Microsoft Access. I believe it requires you to place parentheses when using multiple Join.

Show 5 more comments

3 answers

2

select  a.chvbfj, b.chvvnda, c.Desc  from rec a
inner join vndB b on b.chvvnda = a.doc
inner join ps c on c.chvps = b.chvps
WHERE a.chvbfj = '3'
  • : 'Syntax error (missing operator) in query expression 'b. chvvnda = a.doc left Join ps c on c.chvps = b.chvps'.'

  • Man, I’m sorry I’m so screwed up... I’m out of SQL here to mount and see the real problem... Try again, please.

  • Also try to take out the Inner, and simply put Join

  • : 'Syntax error in the FROM clause.' error

  • The name of the tables you have is exactly the same in the above example? fields...everything?

  • is wrong c.Desc, right c.Dsc

Show 1 more comment

1

SELECT * FROM Table 1
INNER JOIN Table2 ON Table2 . id = Table1.id
INNER JOIN Table3 ON Table3 = Table1 . id

0

SELECT C.Desc FROM rec A
JOIN vndB B on B.chvvnda = A.doc
JOIN ps C ON C.chvps = B.chvps 

After that search the data you want with the conditions you want.

  • 'Syntax error in the FROM clause.'

  • Try again, I got the table name ps

  • : 'Syntax error in FROM clause.' same error

  • I changed it again, check the table names if they match the ones you have.

  • : 'Syntax error in clause FROM.' Voce puts in select C.Dsc FROM rec A --- but C.Dsc eh from table ps

  • wrong=C.Desc Correct=C.Dsc

Show 1 more comment

Browser other questions tagged

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