首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能在SQL语句中使用分号。我该怎么办呢?

不能在SQL语句中使用分号。我该怎么办呢?
EN

Stack Overflow用户
提问于 2016-07-05 06:52:56
回答 3查看 372关注 0票数 2

sql接口不允许我在SQL查询中添加分号。我该怎么做才能避开这一切?

代码语言:javascript
运行
复制
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程序中,没有;,查询工作得很好。

EN

回答 3

Stack Overflow用户

发布于 2016-07-05 06:55:29

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

代码语言:javascript
运行
复制
order by country_count desc
limit 10;

请注意,这是MySQL语法,所以这个查询引发错误并不奇怪。您应该使用rownum

代码语言:javascript
运行
复制
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;
票数 2
EN

Stack Overflow用户

发布于 2016-07-05 06:56:44

你在找这个

代码语言:javascript
运行
复制
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来限制记录

票数 2
EN

Stack Overflow用户

发布于 2016-07-05 07:29:53

如果您有Oracle12,可以使用先取

代码语言:javascript
运行
复制
     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

如果没有,实际上首先获取的是语法糖:

代码语言:javascript
运行
复制
  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版本< 12中使用它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38196970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档