【发布时间】:2017-09-20 14:35:04
【问题描述】:
数据库:
Car: Contract: Customer:
Carplate (PRKEY) Contractid(PRKEY) CustomerID(PRIK)
PRICE Miles Customername
Leased(0Free- 1Leased) Carplate(FKEY) Zipcode
ContractID FKEY)
Carplate(FKEY)
我想获取所有租车 (=1) 的当前里程、客户名和分组方式(错误排序方式)客户的邮政编码。
语法
SELECT *
FROM table1 INNER JOIN table2 ON
table1.primaryKey=table2.FKEY INNER JOIN
table3 ON table2.primaryKey=table3.FKEY
问题:我不明白这里的 INNER JOIN 和 where 子句的正确用法。
Select Car.Leased, Contract.Miles, Customer.Customername, Customer.Zipcode
From Car
INNER JOIN Contract on Car.Carplate = Contract.Carplate
INNER JOIN Customer on Contract.Carplate = Customer.Carplate
where Car.Leased = 1
order by Customer.Zipcode ASC
尝试通过语法创建代码。对吗?
【问题讨论】:
-
看起来还不错。你得到预期的结果了吗?
-
JOINs最终会根据其类型创建所涉及表的行的几种组合 -INNER, OUTER, LEFT, RIGHT,等。where子句过滤掉相关的。 -
我在纸上学习。
-
关于数据库本身的明显问题:如果
customer表是contract的子表,则基于contractid,而contract是car的子表,基于carplate,为什么您也在customer中有carplate?对汽车的提及是否意味着客户与合同中的汽车相关联?如果是这样,您需要说明应该使用从客户到汽车的哪个链接。 (更可能的是,情况并非如此 - 客户唯一的汽车是合同中的汽车,您不需要customer表中的carplate。)
标签: sql oracle select sql-order-by where