我做了一个查询,从中我得到了产品及其图像。在产品具有图像的情况下,查询工作正常。但它没有得到没有图像的产品。我们如何以这样一种方式进行此查询:获取没有图像的产品,并且在这种情况下使用默认图像用于所有这些产品?查询是
SELECT u.f_name,
u.l_name,
u.contact,
u.user_id,
u.address,
u.city,
u.org_name,
u.website,
p.id AS product_id,
p.name AS product_name,
p.price AS product_price,
p.quantity AS product_quantity,
p.description AS description,
g.photo_url AS product_image
FROM user_profile u,
product p,
gallery g
WHERE p.category_id='1'
AND p.subcategory_id='2'
AND p.seller_id=u.user_id
AND g.main_id=p.id
AND g.type_id='2'
GROUP BY p.name
答案 0 :(得分:1)
SELECT u.f_name,
u.l_name,
u.contact,
u.user_id,
u.address,
u.city,
u.org_name,
u.website,
p.id AS product_id,
p.name AS product_name,
p.price AS product_price,
p.quantity AS product_quantity,
p.description AS description,
CASE WHEN g.photo_url IS NULL THEN 'some_default_url' ELSE g.photo_url END AS product_image
FROM user_profile u
RIGHT JOIN
product p ON p.seller_id=u.user_id
JOIN
gallery g ON g.main_id=p.id
AND p.category_id='1'
AND p.subcategory_id='2'
AND g.type_id='2'
GROUP BY p.name
您可能希望在某些列上放置一些聚合函数,以获得更可控的结果。
对于默认图像
CASE WHEN g.photo_url IS NULL THEN 'some_default_url' END AS product_image