mysql - 如何从订单中获取产品、订单条目、从查询中获取订单状态
问题描述
我准备了一个灵活的搜索查询。在这里,我开发了一个条件,如:
订单状态在订单中完成
以及按顺序出现的订单条目
获取 orderentries 中的产品
为此我写了一个查询
select {p.pk} from {
order as o
join OrderStatus as os on {os.pk}={o.status}
join orderentry as oe on{oe.order}={o.pk}
join product as p on {oe.product}={p.pk}
}
where {os.code}='COMPLETED'
AND {o.date}>'2020-08-16 00:00:00.000' AND{o.date}<'2020-09-30 00:00:00.000'
group by{p.pk} order by count({oe.pk}) desc limit 10
在这个查询中,我想要的是我想获取所有产品信息,比如
select * from Product}
如何修改此查询获取所有产品?
解决方案
您可以使用子选择来执行此操作。您在上面发布的第一个查询将是子选择。您只需添加另一个选择即可获取子选择中返回的所有 PK 的产品信息。
select * from {Product as prod} where {prod.pk} in
({{
select
top 10 {p.pk}
from
{
Order as o join
OrderStatus as os on {os.pk} = {o.status} join
OrderEntry as oe on {oe.order} = {o.pk} join
Product as p on {oe.product} = {p.pk}
}
where
{os.code} = 'COMPLETED' and
{o.date} > '2020-08-16 00:00:00.000' and
{o.date} < '2020-09-30 00:00:00.000'
group by {p.pk}
order by count({oe.pk}) desc
}})
推荐阅读
- node.js - 刷新页面后如何让用户保持登录状态?
- javascript - 如何使用 java 脚本查询选择器根据父元素选择子元素
- r - RenderTable Shiny 中数据表中的小数问题
- python - 如何在Python中按数组顺序随机获取数组
- r - R {targets} 包:如何使用字符串引用现有目标?
- python - 如何在 Dash Python 中使用本地字体(.otf 文件)
- python - 如何通过 python 与 ftdi f232rl 模块正确通信?
- sas - 如何在 sas 企业指南中开发配置文件?
- sql - 主查询列中的 SQL 查询子表
- php - 为什么 modal 不能与 PHP 一起用于 show 几句话