set @n=(select areaid from areas where areaname='江苏省'); with tmp as (select customerid,sum(amount) amt from orders join orderitems using(orderid) group by customerid order by amt desc) select * from customers a join areas b on a.regionid=b.areaid where b.regionid=@n and customerid in CEILING((SELECT COUNT(*) FROM tmp) * 0.3);语句哪里错误
时间: 2023-06-12 14:04:48 浏览: 124
省市区三级联动下拉框菜单javascript版
这个 SQL 语句有几个问题:
1. `CEILING` 函数需要一个数字作为参数,但是在这个语句中,你试图将子查询作为参数传递给它。你需要使用括号将子查询括起来,以便它可以返回一个数字。
2. `regionid` 列在 `areas` 表中,但是在查询中你使用了 `areaid` 这个列名。你需要将这个列名更正。
下面是修正后的 SQL 语句:
```
set @n=(select areaid from areas where areaname='江苏省');
with tmp as (
select customerid, sum(amount) amt
from orders
join orderitems using(orderid)
group by customerid
order by amt desc
)
select *
from customers a
join areas b on a.regionid=b.regionid
where b.regionid=@n and customerid in (SELECT CEILING(COUNT(*) * 0.3) FROM tmp);
```
这个语句会返回在江苏省地区排名前 30% 的客户的信息。
阅读全文