【问题标题】:Syntax errors in MS Access converting from SQL从 SQL 转换的 MS Access 中的语法错误
【发布时间】:2013-06-18 06:10:57
【问题描述】:

从我之前的问题 subqueries-on-subqueries

我现在正在尝试将其放入访问权限

代码来自链接上的答案一,完全复制并粘贴,除了放置 left(citycode,2) 而不是 substring(citycode,2)

现在尝试运行它时出现语法错误。

第一个是连接操作中的语法错误

做了一些研究并更改了代码。然后是不支持JOIN表达式

我看了这篇文章"JOIN expression not supported" error caused by unbracketed JOIN expression comprising string condition

现在我想我已经把它缩小到查询表达式'province = x.provincecode and customers.city = x.citycode where category like 'SC'中的语法错误(缺少运算符)

现在这一切都可以在 SQL 中运行。我需要在访问中使用它,因为我的数据库是 .mdb。 输出应该只有一行:

我不知道我的陈述中的错误在哪里

SELECT * from (SELECT * from City  where Provincecode like 'EC' and citycode in 
(select citycode from city  where left(citycode,2) not like 'bx')) 
as x inner join customers on (province = x.provincecode and
customers.city=x.citycode where category like 'SC')

正如我所说,这在 SQL 中有效,并且只需要一行代码。 但是一到访问就出错了

【问题讨论】:

    标签: sql database ms-access join syntax-error


    【解决方案1】:

    也许这个(我没有数据要检查):

    SELECT * 
    from (
        SELECT * 
        from City  
        where Provincecode like 'EC' and citycode in (
            select citycode 
            from city  
            where left(citycode,2) not like 'bx'
        )
    ) as x 
    inner join customers on customers.province = x.provincecode and customers.city=x.citycode
    where x.category like 'SC'
    

    这里不需要括号,你只使用了单连接。如果你有更多的连接,那么一般的语法是:

    select ...
    from ((
        table1
        join table2 on ...)
        join table3 on ...)
        join table4 on ...
    where ...
    

    【讨论】:

      【解决方案2】:

      将以下内容保存为查询: SELECT * from City where Provincecode like 'EC' and citycode in (从左边的城市中选择城市代码(citycode,2)不像'bx')

      然后编写第二个查询,将第一个查询与客户表连接起来

      顺便说一句:当您使用 LIKE 时,您必须使用通配符,例如 * 或 ?

      【讨论】:

      • 对不起。你能详细说明一下吗?我尝试了SELECT * from Query1 inner join customers on(province likeprovincecode and customers.city like citycode where category like 'SC')。这是正确的吗?对不起,没有使用访问是年龄。客户想要访问数据库
      • 您应该使用 Access 查询设计器来执行此操作。这样 Access 将为您编写 SQL 语法。
      【解决方案3】:

      我不确定你为什么在 city 表上执行第一个子选择来应用第二个子句,我可能忽略了一些东西,但是这个查询是否足够:

      SELECT A.*
      FROM City As A, Customers As B
      WHERE A.Provincecode like 'EC*'
      AND Left(A.citycode, 2) not like 'bx*'
      AND B.category like 'SC*'
      AND B.province = A.provincecode
      AND B.city = A.citycode
      

      【讨论】:

      • @NicholasAysen 我不知道你为什么改变了接受,我也试图通过证明你不需要嵌套子查询来帮助你,值得理解 SQL 和学习最佳实践,因为通常子查询会降低性能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多