sql - 如何在sql中将4个表连接在一起
问题描述
我有客户、产品、订单orderdetails 4 表。他们的关系是这样的。反应
我的 SQL 代码如下所示:
select customers.CUSTOMERNAME, customers.STATE, customers.creditlimit
from customers
INNER JOIN ORDERS ON customers.customernumber = ORDERS.CUSTOMERNUMBER;
select ORDERDETAILS.PRICEEACH, ORDERDETAILS.QUANTITYORDERED
from ORDERDETAILS
inner join orders on ORDERS.ORDERNUMBER = ORDERDETAILS.ordernumber;
select products.productname
from products
inner join orderdetails on orderdetails.productcode = products.productcode;
我想将 4 个表加入到一个带有选定段的表中。我怎样才能做到这一点?
解决方案
您可以执行以下操作
SELECT c.CUSTOMERNAME
, o.CUSTOMERNUMBER
, od.PRICEEACH
, p.productname
FROM customers c
INNER JOIN orders o
on c.customernumber = o.customernumber
INNER JOIN ORDERDETAILS od
on o.ORDERNUMBER = od.ORDERNUMBER
INNER JOIN products p
on od.productcode = p.productcode
推荐阅读
- .net-core - 注入服务以配置 SignalR 背板
- android - 传递来自 API 调用的错误
- python - Django 使用查询集对象的属性
- openlayers - OpenLayers 6 - 覆盖定位不起作用
- android - Android 3rd 方权限中的签名权限
- python - 使用 Pyinstaller 构建的 Tensorflow GUI 可执行文件无法运行
- php - 为什么在此 JSON 字符串上使用 json_decode 会导致空值
- qt - Qt Quick Actions 未显示彩色图标
- android - Flutter 测试完成但出现错误:ProcessException: Permission denied
- javascript - 在不同页面上使用变量更改图像 src 的正确方法是什么?