从数据库获取产品

发布于 2024-09-13 04:47:07 字数 920 浏览 7 评论 0原文

我正在尝试通过单个查询从数据库中获取所有产品。我陷入了价格部分:

VirtueMart 有一个名为 #__vm_product 的表和另一个名为 #__vm_product_price 的表。

如果产品有父产品,则意味着该产品继承父产品的所有内容,除非在子产品中设置不同。

这些表如下所示:

/* #__vm_product PARTIAL */
int - product_id
int - product_parent_id
varchar - product_name

/* #__vm_product_price PARTIAL */
int - product_id
decimal - product_price
int - mdate

我进行了下一个查询,该查询获取所有产品及其价格:

SELECT
p.product_id AS id,
product_name AS name,
product_price AS price,
p.product_parent_id AS parent,
MAX(pp.mdate) AS last_updated

FROM jos_vm_product p
LEFT JOIN jos_vm_product_price pp ON p.product_id = pp.product_id

GROUP BY p.product_id
ORDER BY p.product_id

该查询的问题是它不检查它们是否是指定的价格。因此,如果它是子产品并且没有价格,则应显示其父产品的价格。

有人可以帮我解决这个问题吗?

注意:如果有人知道从 VirtueMart 数据库获取所有产品(带价格)的更简单方法,请不要介意告诉我:)

编辑:价格永远不为空。如果子级应该从其父级继承,那么它在 jos_vm_product_price 中没有价格行

I'm trying to get all products from the database with a single query. I get stuck at the price part:

VirtueMart has a table called #__vm_product and another one called #__vm_product_price.

If a product has a parent product, it means that product inherits everything from the parent unless it's set different in the child.

The tables look like this:

/* #__vm_product PARTIAL */
int - product_id
int - product_parent_id
varchar - product_name

/* #__vm_product_price PARTIAL */
int - product_id
decimal - product_price
int - mdate

I made the next query which gets all products with their price:

SELECT
p.product_id AS id,
product_name AS name,
product_price AS price,
p.product_parent_id AS parent,
MAX(pp.mdate) AS last_updated

FROM jos_vm_product p
LEFT JOIN jos_vm_product_price pp ON p.product_id = pp.product_id

GROUP BY p.product_id
ORDER BY p.product_id

The problem with this query is that it doesn't check if their is a price specified. So if it's a child product and it has no price, it should show the price of it's parent.

Could someone help me out with this?

Note: If anyone knows an easier way to get all products (with price) from the VirtueMart database, please don't mind to tell me :)

EDIT: Price is never null. If child is supposed to inherit from it's parent it just doesn't have a price row in jos_vm_product_price

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瓜 2024-09-20 04:47:07

更新:

select
    p.product_id AS id, 
    p.product_name AS name, 
    coalesce(pp.product_price, pp2.product_price) AS price, 
    p.product_parent_id AS parent, 
    coalesce(pp.mdate, pp2.mdate) AS last_updated 
from jos_vm_product p 
left outer join jos_vm_product p2 on p.product_parent_id = p2.product_id
left outer join (
    select product_id, max(mdate) as maxmdate
    from jos_vm_product_price
    group by product_id
) ppm on p.product_id = ppm.product_id
left outer join jos_vm_product_price pp on ppm.product_id = pp.product_id and ppm.maxmdate = pp.mdate 
left outer join (
    select product_id, max(mdate) as maxmdate
    from jos_vm_product_price
    group by product_id
) ppm2 on p2.product_id = ppm2.product_id
left outer join jos_vm_product_price pp2 on ppm2.product_id = pp2.product_id and ppm2.maxmdate = pp2.mdate 

Updated:

select
    p.product_id AS id, 
    p.product_name AS name, 
    coalesce(pp.product_price, pp2.product_price) AS price, 
    p.product_parent_id AS parent, 
    coalesce(pp.mdate, pp2.mdate) AS last_updated 
from jos_vm_product p 
left outer join jos_vm_product p2 on p.product_parent_id = p2.product_id
left outer join (
    select product_id, max(mdate) as maxmdate
    from jos_vm_product_price
    group by product_id
) ppm on p.product_id = ppm.product_id
left outer join jos_vm_product_price pp on ppm.product_id = pp.product_id and ppm.maxmdate = pp.mdate 
left outer join (
    select product_id, max(mdate) as maxmdate
    from jos_vm_product_price
    group by product_id
) ppm2 on p2.product_id = ppm2.product_id
left outer join jos_vm_product_price pp2 on ppm2.product_id = pp2.product_id and ppm2.maxmdate = pp2.mdate 
北城孤痞 2024-09-20 04:47:07

您可以使用 CASE 语句检查子项的产品价格,如果为空,则使用父项价格。

另外,您需要另一个加入才能获得父级。

    SELECT
        p.product_id AS id,
        product_name AS name,
        CASE
            WHEN pp.product_price IS NULL then pp2.product_price
            ELSE pp.product_price
        END AS price,
        p.product_parent_id AS parent,
        MAX(pp.mdate) AS last_updated

    FROM jos_vm_product p
    LEFT OUTER JOIN jos_vm_product_price pp
           ON p.product_id = pp.product_id
       , jos_vm_product parent
       , jos_vm_product_price pp2
   WHERE (p.product_parent_id = parent.product_id OR p.product_parent_id IS NULL)
     AND (parent.product_id = pp2.product_id OR p.product_parent_id IS NULL)
    GROUP BY
        p.product_id,
        product_name,
        CASE
           WHEN pp.product_price IS NULL then pp2.product_price
           ELSE pp.product_price
        END,
        p.product_parent_id

You can use a CASE statement to check the product price for the child and if it's null, use the parent price.

Also, you need another join to get the parent.

    SELECT
        p.product_id AS id,
        product_name AS name,
        CASE
            WHEN pp.product_price IS NULL then pp2.product_price
            ELSE pp.product_price
        END AS price,
        p.product_parent_id AS parent,
        MAX(pp.mdate) AS last_updated

    FROM jos_vm_product p
    LEFT OUTER JOIN jos_vm_product_price pp
           ON p.product_id = pp.product_id
       , jos_vm_product parent
       , jos_vm_product_price pp2
   WHERE (p.product_parent_id = parent.product_id OR p.product_parent_id IS NULL)
     AND (parent.product_id = pp2.product_id OR p.product_parent_id IS NULL)
    GROUP BY
        p.product_id,
        product_name,
        CASE
           WHEN pp.product_price IS NULL then pp2.product_price
           ELSE pp.product_price
        END,
        p.product_parent_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文