Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What I'm trying to do is to select all 4 of those tables with join but i can't figure it out how because there isn't a table connected to all of the others.

  create table Encomenda(
    idEncomenda int identity,
    idFornededor int not null,
    estado varchar not null,
    Constraint pk_Encomenda Primary Key (idEncomenda),
    );

    create table Produto_Encomenda(
    idProduto_Encomenda int identity,

    idProduto int not null,
    idEncomenda int not null,
    quantidade int not null,

    constraint pk_Produto_Encomenda Primary Key (idProduto_Encomenda),
    constraint fk_Produto foreign key (idProduto) references Produto (idProduto) ,
    constraint fk_idEncomenda foreign key (idEncomenda) references Encomenda (idEncomenda) ,
    );
    create table Fornecedor(
    idFornecedor int  identity,
    nomeFornecedor varchar(60) not null,
    moradaFornecedor varchar(60) not null,
    contactoFornecedor int not null,
    constraint pk_Fornecedor Primary Key (idFornecedor),
    );
    create table Produto(
    idProduto int identity,
    nomeProduto varchar(60) not null,
    quantidadeExistenteProduto int not null,
    precoUnidade float not null,
    Constraint pk_produto Primary Key (idProduto),
    );

I was trying to make a join between the 4 of them and what I would like to show/select are:

Fornecedor.nomeFornecedor, idEncomenda, Produto.nomeProduto and Produto_encomenda.quantidade" joined toguether where 
Produto.idproduto = produto_Encomenda.idproduto
Fornecedor.idFornecedor = Encomenda.idFornecedor

I don't think I can explain better but in the end I wanted to select a table that containsFornecedor.nomeFornecedor, idEncomenda, Produto.nomeProduto and Produto_encomenda.quantidade, but because the 4 tables dont have 1 common table im lost in how to make the join : im probably just tired as hell but if someone could help me i would apreciatte cuz im so lost here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

Ok, now that I think I better understand the question you need the following fields: Fornecedor.nomeFornecedor, idEncomenda, Produto.nomeProduto and Produto_encomenda.quantidade.

So, let's see if this works:

SELECT f.nomeFornecedor, 
e.idEncomenda, 
p.nomeProduto,
pe.quantidade
FROM Fornecedor as f
INNER JOIN Encomenda AS e
ON f.idFornecedor = e.idFornededor 
INNER JOIN Produto_Encomenda as pe
ON e.idEncomenda = pe.idEncomenda
INNER JOIN Produto as p
ON p.idProduto = pe.idProduto

I think this should work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...