我准备了一个灵活的搜索查询。在这里,我出现了一种情况,比如:
订单状态在订单中完成
以及订单中存在的订单条目
获取订单条目中的产品
为此,我写了一个查询
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}
如何修改此查询以获取所有产品?
您可以使用subselect来完成此操作。您在上面发布的第一个查询将是子选择。您只需添加另一个select即可获取子select中返回的所有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
}})