有关列标题的SQL查询

时间:2013-03-18 17:32:22

标签: sql

我目前正在SQL中创建一个查询,我需要在Car For Sale表中生成一个当前没有汽车销售的客户列表。 到目前为止我的代码如下:

SELECT 
  Customer.CustomerCode, CarForSale.CustomerCode
FROM 
  Customer LEFT JOIN CarForSale
  ON Customer.CustomerCode = CarForSale.CustomerCode
WHERE 
  CarForSale.CustomerCode IS NULL;

我遇到的问题是显示的正确客户代码不在Car for Sale表中,但是我需要将Column标题显示为'CustomerCode'。

有谁知道我可以添加什么来产生这个?

2 个答案:

答案 0 :(得分:0)

SELECT Customer.CustomerCode As [CustomerCode], CarForSale.CustomerCode
FROM Customer 
LEFT JOIN CarForSale ON Customer.CustomerCode = CarForSale.CustomerCode 
WHERE CarForSale.CustomerCode IS NULL

会奏效。 (如果使用T-SQL)

答案 1 :(得分:0)

SELECT
  *
FROM
(
SELECT 
  Customer.CustomerCode CustomerCodeA, CarForSale.CustomerCode CustomerCodeB
FROM 
  Customer LEFT JOIN CarForSale
  ON Customer.CustomerCode = CarForSale.CustomerCode
)
WHERE 
  CustomerCodeB IS NULL;