Home > Software design >  2 data sets, need to write a query that involves specific components from both
2 data sets, need to write a query that involves specific components from both

Time:06-22

id product_name
A almond
B broccoli
D durian
F fries

CodePudding user response:

If I understand your question correctly, then try using CASE in the select statement.

SELECT CASE 
           WHEN pn.product_name IS NULL THEN 'Other Product'
           ELSE pn.product_name END AS ProductName, 
       SUM(p.quantity) AS Quantity
FROM Products p
LEFT JOIN ProductName pn ON p.id = pn.id
GROUP BY pn.product_name

What it's doing is, if it finds product_name as null then it will select the name as 'Other Product', otherwise it will show the corresponding Product Name.

  • Related