- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个多对多关系,我想选择特定字段的总和以及所有必要的数据。我将通过代码解释我的案例。
首先是相关的表结构:
CREATE TABLE `products` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` text,
`description` text,
`price` float,
`picture` varchar(255)
);
CREATE TABLE `orders_products` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`quantity` integer,
`status` boolean,
`product_id` integer,
`order_id` integer,
FOREIGN KEY(`product_id`) REFERENCES "products" ( "id" ),
FOREIGN KEY(`order_id`) REFERENCES "orders" ( "id" )
);
CREATE TABLE `orders` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`created_at` datetime,
`updated_at` datetime,
`user_id` integer,
FOREIGN KEY(`user_id`) REFERENCES "users" ( "id" )
);
基本上我现在所做的是:
select
"products".name,
"products".price,
"orders_products"."order_id",
"orders_products"."product_id"
from "products"
inner join "orders_products" on "orders_products"."product_id" = "products"."id"
where "orders_products"."order_id" in (1, 2)
这会产生以下结果:
name price order_id product_id
"coca cola" "2.35" "1" "1"
"snickers" "1.25" "1" "2"
"snickers" "1.25" "2" "2"
"popcorn" "1.19" "2" "3"
但是,如果我想“求和”价格,并根据 order_id 进行排序:
select
sum ("products".price) as _sum,
"products".name,
"products".price,
"orders_products"."order_id",
"orders_products"."product_id"
from "products"
inner join "orders_products" on "orders_products"."product_id" = "products"."id"
where "orders_products"."order_id" in (1, 2)
group by "orders_products"."order_id"
我得到以下结果:
_sum name price order_id product_id
"3.6" "snickers" "1.25" "1" "2"
"2.44" "popcorn" "1.19" "2" "3"
我真正想要的是:
name price order_id product_id _sum
"coca cola" "2.35" "1" "1" "3.6"
"snickers" "1.25" "1" "2" "3.6"
"snickers" "1.25" "2" "2" "2.44"
"popcorn" "1.19" "2" "3" "2.44"
这可能吗?
最佳答案
您可以做的是将每个 order id
的 sums
投影为派生表,然后将您的原始查询连接回派生表,这将允许您将 sum
显示为每个订单的列:
select
p.name,
p.price,
op.order_id,
op.product_id,
Totals.theSum
from products p
inner join orders_products op on op.product_id = p.id
inner join
(select sum(p.price) as theSum,
op.order_id
from products p
inner join orders_products op on op.product_id = p.id
where op.order_id in (1, 2)
group by op.order_id) AS Totals
on Totals.order_id = op.order_id;
内部派生表已经完成过滤,不再重复。
关于SQL 查询 : how to add "sum" along with the corresponding data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922822/
git count-objects -v 发出的这个警告是什么意思? Google 只给了我 git 本身的来源。 warning: no corresponding .pack: .git/obje
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
假设您将一个项目分解为一个 super 项目和一个子模块。它们都有稳定的(主)分支和开发分支。有没有办法设置您的子模块,以便 super 项目从其 dev 分支跟踪子模块的 dev 分支,并从其稳定分
我有一个列表,其中包含两种不同的商品,product-x 和 product-y。 当用户删除 product-x 时,我需要它的等效 product-y 也删除,反之亦然。 我已经尝试了几种不同的方
下面的示例显示了一个 box div 和一个 :after 内容,它应该是一个单独的 block 。 div.box { background-color: #FAA; width:
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我使用的是 ABAP 7.4 SP13。 我想在下面的代码中使用“选项 2”,但是如何进行这种深度映射?这里的目标是有一个干净、简单、易于阅读的映射,避免循环。 TYPES: BEGIN OF
我试图充分了解 docker 的功能,以便对我安全地使用它产生合理的信心。对此的一个建议是始终使用 USER Dockerfile 中的语句。在试图理解这一点的影响时,我遇到了一些麻烦。 具体问题:
我在 Flutter Web 应用程序中使用命名路由进行导航。导航到所需的路由时,URL 会更新,但我无法通过 URL 栏直接导航到该路由。每次我尝试在 URL 中添加路径时,它都会将我带到“.../
我重命名了一个文件夹并更新了命名空间声明,但 ReSharper 6 声称命名空间应该反射(reflect)重命名之前的情况。文件位置数据存储在哪里? 最佳答案 检查并确保您的程序集名称与新的命名空间
我正在 Shopify 上创建带有折扣的草稿订单,其返回错误类似于“必须与根据值计算出的值相对应”。 我计算折扣如下: $amount 是订单总金额 (78.99),$rate (30) 是折扣百分比
我正在使用pyspark.ml.classification中的RandomForestClassifier 我在二进制类数据集上运行模型并显示概率。 我的列概率如下: +-----+--------
我正在尝试绘制以不同角度记录的传感器数据。 import pandas as pd import matplotlib.pyplot as plt #create dataframe, each
我的 ASP.NET 页面没有显示控件。它只是显示母版页错误 The page has one or more controls that do not correspond with 代替 关
问题: 我有一大堆日期,我需要按周排列它们。 问题:如何按日期所在的一年中的第几周对日期进行分组? 示例数据集 Date date = new SimpleDateFormat.parse("04/0
我想获取此命令的选项: my_cmd show --value true -D 为此,我需要通过选项两次(由于架构)。在第二遍期间,无法正确检索与其选项对应的参数。在此示例中,在第二次传递期间检索到的
本地代码 native .c #include #include #include jstring Java_com_lab5_oli_myapplication_MainActivity_he
我正在使用 Firebase Python AdminSDK 生成自定义 token ,Javascript 客户端使用该 token 登录 Firebase。当 JS 客户端尝试使用自定义 toke
在上面的 excel 表中,我需要一个公式来提取存在“1”的特定列名,然后在相应的单元格中输入该名称。示例 - 在上面的图像中,对应于 col“Tags”的每一行都包含相应单元格包含“1”的列名。 最
我是一名优秀的程序员,十分优秀!