首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL查询汇总

MySQL查询汇总
EN

Stack Overflow用户
提问于 2022-01-10 10:42:06
回答 1查看 38关注 0票数 1

我有个问题。我的MySQL查询不起作用。我怎么才能解决这个问题?我的疑问

代码语言:javascript
复制
select state, city, sum((sales.retail_price - products.wholesale_price) * sales.quantity) as profit 
from products, sales
where sales.product_id = products.product_id
group by rollup (state, city)
order by state, city;

我的错误

代码语言:javascript
复制
11:39:12    select state, city, sum((sales.retail_price - products.wholesale_price) * sales.quantity) as profit  from products, sales where sales.product_id = products.product_id group by rollup (state, city) order by state, city   Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(state, city) order by state, city' at line 4  0.000 sec

我的模式

代码语言:javascript
复制
-- Create some tables and insert some rows.
create table products (product_id integer, wholesale_price real);
insert into products (product_id, wholesale_price) values 
    (1, 1.00),
    (2, 2.00);

create table sales (product_id integer, retail_price real, 
    quantity integer, city varchar, state varchar);
insert into sales (product_id, retail_price, quantity, city, state) values 
    (1, 2.00,  1, 'SF', 'CA'),
    (1, 2.00,  2, 'SJ', 'CA'),
    (2, 5.00,  4, 'SF', 'CA'),
    (2, 5.00,  8, 'SJ', 'CA'),
    (2, 5.00, 16, 'Miami', 'FL'),
    (2, 5.00, 32, 'Orlando', 'FL'),
    (2, 5.00, 64, 'SJ', 'PR');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-10 10:44:47

试试这个:

代码语言:javascript
复制
select state, city, sum((sales.retail_price - products.wholesale_price) * 
sales.quantity) as profit 
from products, sales
where sales.product_id = products.product_id
group by state, city WITH ROLLUP
order by state, city;

你可以看到这里的卷起行为

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

https://stackoverflow.com/questions/70651113

复制
相关文章

相似问题

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