- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个产品表,我想根据我的产品表获取订购产品的总数
这是我的产品表
products
+---------------+-------------------+
| product_id | product_name |
+---------------+-------------------+
| 1 | ice cream |
| 2 | iced tea |
| 3 | cake |
| 4 | orange juice |
| 5 | coconut drink |
| 5 | coconut shake |
+---------------+-------------------+
这是我的 order_items 表
+---------------+-------------------+----------------+---------------+
| order_id | product_name | product_qty | product_id |
+---------------+-------------------+----------------+---------------+
| 1 | ice cream | 5 | 1 |
| 1 | iced tea | 10 | 2 |
| 1 | cake | 10 | 3 |
| 1 | orange juice | 10 | 4 |
| 2 | ice cream | 5 | 1 |
| 2 | iced tea | 10 | 2 |
| 2 | cake | 10 | 3 |
| 2 | orange juice | 10 | 4 |
| 3 | ice cream | 5 | 1 |
| 3 | iced tea | 10 | 2 |
| 3 | cake | 10 | 3 |
| 3 | orange juice | 10 | 4 |
+---------------+-------------------+----------------+---------------+
我的预期结果是这样的
ice cream = 15
iced tea = 30
cake = 30
orange juice = 30
coconut drink = 0
coconut shake = 0
我尝试使用此代码作为引用,但我对内部联接不太了解
SELECT t.timeasd
from time t
left join (
select time_reserved,
date_reservation
from reservations
where date_reservation = '$res_date'
) as q
on t.timeasd = q.time_reserved
where q.date_reservation is null
AND t.status='active';
最佳答案
你只是想要聚合吗?
select product_name, sum(product_qty) sum_product_qty
from order_items
group by product_id, product_name
旁注:您的模型未正确标准化。您不应该将产品名称存储在订单表中:此信息属于产品表。您应该仅将产品的 id 存储在订单表中,然后您将通过连接获得结果:
select p.product_name, coalesce(sum(o.product_qty), 0) sum_product_qty
from products p
left join order_items o on p.product_id = o.product_id
group by p.product_id, p.product_name
关于mysql - 获取 DBtable 上每个项目的总和并将其与另一个 DBtable 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59109663/
标签: 属性: **headList** $scope.headList =[ {name:"运单号",code:"trackingNum",width:"100"}, {name:"件
我有一个产品表,我想根据我的产品表获取订购产品的总数 这是我的产品表 products +---------------+-------------------+ | product_id |
我想检查字段“number_field”中的记录值是否唯一,因此我在 number_field 中查找具有相同值的记录。我使用 Lookup 方法,因为我正在编辑记录: id := SomeTable
我在运行程序时遇到问题。我的代码太旧了,我不确定它是如何工作的。 Cannot resolve unit name 'DBTables' at line 20 Underclared indetifi
我想使用 Spark 处理来自 JDBC 源的一些数据。但首先,我不想从 JDBC 读取原始表,而是想在 JDBC 端运行一些查询来过滤列和连接表,并将查询结果作为表加载到 Spark SQL 中。
目前,我收到了一个普通的 DbTable Auth Adapter : protected function _getAuthAdapter($formData) { $dbAda
每天我都会在日志文件中看到这个错误: PHP Warning: Missing argument 2 for wpdb::prepare(), called in /home/xxxxxxxx/pub
我是一名优秀的程序员,十分优秀!