我在作业中遇到了一个问题:
编写一份列出库存项目名称,制造商的SQL语句 位置城市名称,仓库位置城市名称和库存项目 以递增的STOCKITEM主键顺序销售价格。清单必须 包括销售价格仅小于或等于20的商品。
注1:您需要使用4个内部联接。
注2:CITY表与WAREHOUSE表和 制造商表。
鉴于表:
CITY 制造商 仓库 STOCKITEM
关系:
1 CITY可能有很多制造商
1 CITY可能有很多WAREHOUSES
1制造商可能有很多STOCKITEMS
1 WAREHOUSE可能有很多STOCKITEMS
以下是我写的代码:
SELECT
stock.STKNAME AS "Stock Name",
stock.SELLINGPRICE AS "Selling Price",
ct.CITYNAME AS "MANUFACTURER CITY",
ct.CITYNAME AS "WAREHOUSe CITY"
FROM
STOCKITEM stock
INNER JOIN WAREHOUSE ware
ON
stock.WHID = ware.WHID
INNER JOIN MANUFACTURER manu
ON
stock.MANID = manu.MANID
INNER JOIN CITY ct
ON
ware.CITYID = ct.CITYID
INNER JOIN CITY ct
ON
manu.CITYID = ct.CITYID
WHERE
stock.SELLINGPRICE <= 20
ORDER BY
stock.STKID ASC;
我的错误:
答案 0 :(得分:3)
你的问题在这里:
INNER JOIN CITY ct
ON ware.CITYID = ct.CITYID
INNER JOIN CITY ct
ON manu.CITYID = ct.CITYID
您使用相同的别名连接同一个表两次,重命名一个,它应该没问题。
答案 1 :(得分:1)
别名ct
正在使用两次。您需要在整个查询中修复此问题:
SELECT stock.STKNAME AS "Stock Name",
stock.SELLINGPRICE AS "Selling Price",
ctm.CITYNAME AS "MANUFACTURER CITY",
---------^
ctw.CITYNAME AS "WAREHOUSe CITY"
---------^
FROM STOCKITEM stock INNER JOIN
WAREHOUSE ware
ON stock.WHID = ware.WHID INNER JOIN
MANUFACTURER manu
ON stock.MANID = manu.MANID INNER JOIN
CITY ctw
------------^
ON ware.CITYID = ctw.CITYID INNER JOIN
------------------------^
CITY ctm
------------^
ON manu.CITYID = ctm.CITYID
------------------------^
WHERE tock.SELLINGPRICE <= 20
ORDER BY stock.STKID ASC;