我试图将2个表值访问为单个查询。对于Eg Customer name,id,Address to single query。客户地址 - 州和地区另一个表中的国家我试图访问查询中单个列的所有地址。仅在Customer表状态和国家/地区代码中存储。现在,我可以查询所有查询中的客户名称,ID,地址(单列),但是能够访问该状态&国家。
SELECT CustomerName, CustomerId, (BFlatNo +','+ BPremises +','+ BStreet +','+ BArea +','+ BLocation +','+ BCity +','+ BState +','+ BCountry) as Address FROM Customer WHERE CustomerId=11;
此代码成功运作&结果这个 得到24和1是国家和国家的身份。国家。
所以我再次修改这段代码,以获得单列状态德里的确切地址,而不是24和国家印度,而不是1
SELECT CustomerName, CustomerId, (BFlatNo +','+ BPremises +','+ BStreet +','+ BArea +','+ BLocation +','+ BCity +','+ BState +','+ (select CountryName from Country where CountryIdId=Customer.Country) AS Country) AS Address FROM Customer;
此代码显示语法错误!怎么解决这个问题?
答案 0 :(得分:2)
如果您想从多个表中SELECT
,请在FROM
子句中包含这些表,或使用JOIN
。
SELECT CustomerName,
CustomerId,
(BFlatNo & ',' & BPremises & ',' & BStreet & ',' & BArea & ',' & BLocation & ',' & BCity & ',' & BState & ',' & CountryName) AS Address
FROM Customer
INNER JOIN
Country
ON Country.CountryId = Customer.Country;
答案 1 :(得分:1)
我不确定MS Access语法与SQL Server,但你可以试一试:
SELECT
CustomerName,
CustomerId,
(
(BFlatNo +','+ BPremises +','+ BStreet +','+ BArea +','+ BLocation +','+ BCity +','+ BState)
+','+
(select top 1 CountryName from Country where CountryIdId=Customer.Country)
)
AS Address
FROM Customer;
基本上你不需要像在子查询中那样说“as Country”,你应该返回前1个结果,因为如果有更多结果,这将导致问题。
答案 2 :(得分:1)
您的查询以解决您的问题
SELECT CustomerName,CustomerId,(BFlatNo +','+ BPremises +','+ BStreet +','+ BArea +','+ BLocation +','+ BCity +','+ BState +','+ (从CountryIdId = Customer.Country的国家/地区选择CountryName)) 来自客户的AS地址;
答案 3 :(得分:1)
你应该这样做
SELECT A.CustomerName,
A.CustomerId,
(A.BFlatNo + ',' + A.BPremises + ',' + A.BStreet + ',' + A.BArea + ',' + A.BLocation + ',' + A.BCity + ',' + A.BState + ',' + B.CountryName) AS Address
FROM Customer A, Country B
WHERE B.CountryId = A.Country;