简体   繁体   中英

Joining 3 Tables without accessing the middle table in SQL

I am trying to List the store number, order month and sku description for all stores. Order them by store number. I need to list columns from only the outside tables, but still need to do the INNER JOIN with the middle table to create the relationship. The only difference here is that I do not want to include any columns from the middle table in the SELECT part of the query. Here is the diagram: and second my present Query在此处输入图像描述

表和查询

create table RETAIL_ORDER (OrderNumber integer, StoreNumber integer, StoreZIP integer, OrderMonth integer, OrderYear integer, OrderTotal integer);

create table SKU_DATA (SKU varchar(200), SKU_Description varchar(200), Department varchar(200), Buyer varchar(200));

create table ORDER_ITEM (OrderNumber integer, SKU integer(200), Quantity integer, Price integer, ExtendedPrice integer);

Answer:

select ro.StoreNumber, ro.OrderMonth, sd.SKU_Description
from
RETAIL_ORDER ro inner join ORDER_ITEM oi on ro.OrderNumber = oi.OrderNumber
inner join SKU_DATA sd on oi.SKU = sd.SKU
order by ro.StoreNumber;

From next time, please make sure you dont put screenshots in the question and provide things in text. Screenshots are fine, but should be supported with text.

Also, always provide sample data and expected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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