我有这些表,关于汽车租赁公司
我正在尝试获取尚未支付租金的客户列表,以及付款表中没有的客户列表。
这是记录
这是我尝试使用的查询
select
Customer.CustNo,
name as name, address as address,
dbo.Car.VehNo as Vehno,
dbo.car.model as model,
dbo.car.condition as 'Before Condition',
dbo.car.[Date Registered] as 'Date Registered',
dbo.Car.rentalCost as 'rentalCost',
dbo.Car.brand as brand,
dbo.Rental.[Date Reneted] as 'Date Rented',
dbo.Rental.[Date Returned] as 'Date Returned',
dbo.Rental.condition as 'After Condition'
from
Customer, Car
inner join
Rental on Customer.CustNo = Rental.CustNo
where
dbo.Payment.rentalNo <> dbo.Rental.rentalNo;
但我收到错误
无法绑定多部分标识符
at
Customer.CustNo
和
dbo.Payment.rentalNo
答案 0 :(得分:1)
请试试这个。客户与关怀之间的关系是否为CustNo?如果不是,则需要相应地修改连接。
SELECT cst.CustNo,
NAME AS NAME,
ADDRESS AS ADDRESS,
cr.VehNo AS Vehno,
cr.model AS model,
cr.condition AS 'Before Condition',
cr.[Date Registered] AS 'Date Registered',
cr.rentalCost AS 'rentalCost',
cr.brand AS brand,
rnt.[Date Reneted] AS 'Date Rented',
rnt.[Date Returned] AS 'Date Returned',
rnt.condition AS 'After Condition'
FROM Customer cst
INNER JOIN Rental rnt
ON cst.CustNo = rnt.CustNo
INNER JOIN Car cr
ON cr.VehNo = rnt.VehNo
LEFT OUTER JOIN Payments pts
ON pts.RentalNo = rnt.RentalNo
WHERE pts.RentalNo IS NULL;
答案 1 :(得分:0)
试试这个:
select
cu.CustNo, cu.name, cu.address,
ca.VehNo, ca.model,
ca.condition as 'Before Condition',
ca.[Date Registered] as 'Date Registered',
ca.rentalCost as 'rentalCost',
ca.brand,
r.[Date Reneted] as 'Date Rented',
r.[Date Returned] as 'Date Returned',
r.condition as 'After Condition'
from Customer cu
inner join Rental r on cu.CustNo = r.CustNo
inner join Car ca on ca.VehNo = r.VehNo
where r.rentalNo not in (select distinct rentalno from payment)
左连接方法也可以使用,但使用IN可能会提供更好的性能。
编辑:添加了一个简短的解释。首先,您选择了所需的列(SELECT部分),指定它们彼此之间的关系(INNER JOIN),最后指定您希望RentalNo不存在于其中的所有行付款表(表示缺少付款的行 - NOT IN
子句)。
列别名的使用是可选的,它们只是用于缩短查询(并且可能更容易阅读)。该查询可能也被声明为:
select
Customer.CustNo, name, address,
Car.VehNo, model,
Car.condition as 'Before Condition',
[Date Registered] as 'Date Registered',
rentalCost,
brand,
[Date Reneted] as 'Date Rented',
[Date Returned] as 'Date Returned',
Rental.condition as 'After Condition'
from Customer
inner join Rental on customer.CustNo = rental.CustNo
inner join Car on car.VehNo = rental.VehNo
where Rental.rentalNo not in (select distinct rentalno from payment)
您需要指定 Table.Column 的唯一时间是在查询中的多个表中存在具有相同名称的列时 - 您必须限定从哪个表中获取数据。此外,只有当您想要更改显示的列的名称/标题时才需要使用作为“新列名称”,使用name as name
毫无意义。