【问题标题】:Syntax error in MS Access Query?MS Access 查询中的语法错误?
【发布时间】:2014-02-25 00:14:41
【问题描述】:

我将 7 列合并为一列,将所有地址合并为一列。最初,位置、城市、州和国家/地区存储代码,其显示值在相应的表中。所以我在下面的这个查询中加入了所有内容。

SELECT CustomerName, CustomerId, ContactPerson, 

((BFlatNo +','+ BPremises +','+ BStreet +','+ BArea) + ',' +
(select LocationName from Location where LocationId = Customer.Location) + ',' +
(select CityName from City where CityId = Customer.City) + ',' +
(select StateName from State where StateId = Customer.State) + ',' +
(select CountryName from Country where CountryId = Customer.Country) + ',' + 
(BMobileNumber) ) AS BillingAddress, 

((DFlatNo +','+ DPremises +','+ DStreet +','+ DArea) + ',' +
 (select LocationName from Location where LocationId = Customer.Location) + ',' +
 (select CityName from City where CityId = Customer.City) + ',' +
 (select StateName from State where StateId = Customer.State) + ',' +
 (select CountryName from Country where CountryId = Customer.Country) + ',' + 
(DPhone) ) AS DeliveryAddress

FROM Customer
WHERE Customer.CustomerId = 11;

在 MS Access 中预览数据表视图时,它要求 Customer.Location、Customer.City、Customer.State 和 Customer .Country 然后显示 CustomerId=11 的结果

单击数据表视图时,我需要显示客户详细信息。 实际上在 Customer 表 Location 中,它的字段名是 BLocation,City 是 BCity,State 是 BState & Country 是 BCountry 但是如果我给 Select LocationName from Location where LocationId=Customer.BLocation 它会显示语法错误。

帮我解决这个问题?

【问题讨论】:

  • 子查询太多。改用 JOIN。从可视化查询设计器开始,逐步实现实际的手写查询。
  • 我什至考虑在 VBA 中创建一个函数,该函数在传递地址的 7 个文本元素时构建完整地址。它不是特别快,但我发现维护和处理空白比在 SQL 查询中创建一个非常混乱的表达式更容易。

标签: sql winforms ms-access


【解决方案1】:

谢谢大家,这段代码运行良好

SELECT CustomerName, CustomerId, ContactPerson,

((BFlatNo +','+ BPremises +','+ BStreet +','+ BArea)+','+
(select LocationName from Location where LocationId=Customer.BLocation)+','+
(select CityName from City where CityId=Customer.BCity)+','+
(select StateName from State where StateId=Customer.BState)+','+
(select CountryName from Country where CountryId=Customer.BCountry)+','+ (BMobileNumber)) AS BillingAddress,

((DFlatNo +','+ DPremises +','+ DStreet +','+ DArea)+','+
(select LocationName from Location where LocationId=Customer.DLocateion)+','+
(select CityName from City where CityId=Customer.DCity)+','+
(select StateName from State where StateId=Customer.DState)+','+
(select CountryName from Country where CountryId=Customer.DCountry)+','+ (DPhone)) AS DeliveryAddress

FROM Customer;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多