简体   繁体   English

在相同和不同表中针对不同ID的多个联接

[英]multiple joins for different ids in same and different tables

I've different tables and I've to fetch data. 我有不同的表,我必须获取数据。

order table

orderId, clientId, buyerId, loadingCompanyId, productTypeId, orderStatusId

user table

userId, userCompanyName

orderStatus table

orderStatusId, orderStatusName,

productType table

productTypeId, productTypeName

so I've these tables. 所以我有这些桌子。 In order table clientId, buyerId and loadingCompanyId are coming from user's table userId and I want to fetch its userCompanyName, orderStatusId is coming from orderStatus table and I want to fetch orderStatusName and last one is productTypeName productTypeId is coming from productType table and want to fetch productTypeName but the problem is with clientId, buyerId and loadingCompanyId these three ids are userId from user table so how I've to fetch client, buyer and loading company names. 在订单表clientId,buyerId和loadingCompanyId来自用户表userId的情况下,我想获取其userCompanyName,orderStatusId从orderStatus表获取,我要获取orderStatusName,最后一个是productTypeName productTypeId从productType表获取并想要获取productTypeName但是问题出在clientId,buyerId和loadingCompanyId这三个id是用户表中的userId,所以我要如何获取客户,买方和加载公司名称。

That are some simple joins with aliases 那是一些带有别名的简单联接

select * 
from order 
join user client on clientId = client.userId
join user buyer on buyerId = buyer.userId
join user company on loadingCompanyId = company.userId
join orderStatus  on orderStatus.orderStatusId= order.orderStatusId
join productType on productType.productTypeId = order.productTypeId
SELECT O.orderId, UserClient.userCompanyName As clientName, UserBuyer.userCompanyName AS buyerName, UserloadingCompany.userCompanyName AS buyerName, OS.orderStatusName, PT.productTypeName
FROM Order O
INNER JOIN user AS UserClient
ON O.clientId = UserClient.userId
INNER JOIN user AS UserBuyer
ON O.buyerId = UserBuyer.userId
INNER JOIN user AS UserloadingCompany
ON O.loadingCompanyId = UserloadingCompany.userId
INNER JOIN orderStatus AS OS
ON O.orderStatusId = OS.orderStatusId
INNER JOIN productType AS PT
ON O.productTypeId = PT.productTypeId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM