从数据库获取产品
我正在尝试通过单个查询从数据库中获取所有产品。我陷入了价格部分:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
Updated:
您可以使用
CASE
语句检查子项的产品价格,如果为空,则使用父项价格。另外,您需要另一个加入才能获得父级。
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.