【问题标题】:Cannot use semicolon in SQL statement. What do I do instead?不能在 SQL 语句中使用分号。我该怎么做?
【发布时间】:2016-07-05 06:52:56
【问题描述】:

sql 接口不允许我在 SQL 查询中使用分号。我该怎么做才能避免这种情况?

select country.country, count(customer.customer_id) as country_count
from customer
join address on customer.address_id = address.address_id
join city on address.city_id = city.city_id
join country on city.country_id = country.country_id
group by country.country_id
order by country_count desc;
limit 10

如果我取消界面给出的分号:

错误:ORA-00907:缺少右括号。

如果我输入界面给出的分号:

错误:ORA-00911:无效字符

在我的 sqllite 程序中,如果没有 ;,查询就可以正常工作。

【问题讨论】:

标签: sql oracle


【解决方案1】:

将分号放在LIMIT 之后,分号总是出现在查询的结尾

order by country_count desc
limit 10;

注意这是 MySQL 语法,所以这个查询抛出错误并不奇怪。你应该使用rownum

SELECT * FROM (
    select country.country, count(customer.customer_id) as country_count
    from customer
    join address on customer.address_id = address.address_id
    join city on address.city_id = city.city_id
    join country on city.country_id = country.country_id
    group by country.country_id
    order by country_count desc) t
WHERE rownum < 11;

【讨论】:

  • 我认为别名t不应该出现
  • 这并不重要,不是吗? @Prdp
  • 在 oracle 中我听说对于派生表我们不应该使用别名的地方可能是错误的 ;)
  • @Prdp - 无论你在哪里听到它都是愚蠢的建议。互联网上到处都是。
  • @prdp 也许建议是“您不能使用AS 关键字创建表或派生表别名”。但是,没有AS 的别名可以正常工作。这是一个常见的错误,因为大多数其他数据库都允许AS 用于列别名和表别名。而且有点不一致,不知道为什么Oracle对列和表有不同的别名规则。
【解决方案2】:

你正在寻找这个

SELECT *
FROM   (SELECT country.country,
               Count(customer.customer_id) AS country_count
        FROM   customer
               JOIN address
                 ON customer.address_id = address.address_id
               JOIN city
                 ON address.city_id = city.city_id
               JOIN country
                 ON city.country_id = country.country_id
        GROUP  BY country.country_id
        ORDER  BY country_count DESC)
WHERE  ROWNUM <= 10 

在 Oracle 中 LIMIT 关键字不支持使用 ROWNUM 来限制记录

【讨论】:

    【解决方案3】:

    如果你有Oracle 12,你可以使用FETCH FIRST

         SELECT country.country, COUNT (customer.customer_id) AS country_count
           FROM customer
                JOIN address ON customer.address_id = address.address_id
                JOIN city ON address.city_id = city.city_id
                JOIN country ON city.country_id = country.country_id
       GROUP BY country.country_id
       ORDER BY country_count DESC
    FETCH FIRST 10 ROWS ONLY
    

    如果不是,实际上 FETCH FIRST 是语法糖:

      SELECT country, country_count
        FROM (  SELECT country.country, 
                       COUNT (customer.customer_id) AS country_count, 
                       ROW_NUMBER () OVER (ORDER BY COUNT (customer.customer_id) DESC) rn
                  FROM customer
                       JOIN address ON customer.address_id = address.address_id
                       JOIN city ON address.city_id = city.city_id
                       JOIN country ON city.country_id = country.country_id
              GROUP BY country.country_id)
       WHERE rn <= 10
    ORDER BY country_count DESC
    

    你可以在Oracle版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 2014-01-23
      • 2021-12-14
      • 2021-05-02
      • 2020-05-31
      • 2023-04-04
      相关资源
      最近更新 更多