1

I am not sure what I am trying to achieve here is called which is why I will try to explain with pictures.

enter image description here

Below is the query that I have got:

SELECT DISTINCT
unspsc_commodity.`name` AS Commodity,
unspsc_code.`code` AS `Code`,
attributes.`value`
FROM
products
INNER JOIN unspsc_code ON products.unspsc_code_id =     unspsc_code.unspsc_code_id
INNER JOIN unspsc_commodity ON unspsc_code.unspsc_commodity_id =   unspsc_commodity.unspsc_commodity_id
INNER JOIN attributes ON products.product_id = attributes.product_id
WHERE attributes.`name` = 'Product Type'

Above query only select unspsc_commodity, unspsc_code and value where attribute.name has 'Product Type'. I would like to select unspsc_commodity, unspsc_code for products where there is no Product Type too. So where there is no Product Type in attribute table the query needs to select just the code and commodity and just output null for value. Is it possible to achieve.

2 Answers 2

1

Change your query to have LEFT JOIN like below and move the WHERE condition in select clause with a CASE expression

SELECT DISTINCT
unspsc_commodity.`name` AS Commodity,
unspsc_code.`code` AS `Code`,
CASE WHEN IFNULL(attributes.`name`,'') = 'Product Type' THEN attributes.`value` ELSE NULL END AS Value
FROM
products
INNER JOIN unspsc_code 
ON products.unspsc_code_id = unspsc_code.unspsc_code_id
INNER JOIN unspsc_commodity 
ON unspsc_code.unspsc_commodity_id = unspsc_commodity.unspsc_commodity_id
LEFT JOIN attributes 
ON products.product_id = attributes.product_id;
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you Rahul that does what I needed. Will you please be able to tell me how can I had another case as I would like to select data where the attribute.name has product type and form factor
@user4676307, it can be like general syntax case when condirtion then value when con2 then val2 else val3 end
I get the case statement but will I have another left joint for attribute table ? LEFT JOIN attributes AS attributes_2 ON products.product_id = attributes_2.product_id
CASE WHEN attributes_2.name = 'Form Factor' THEN attributes_2.value ELSE NULL END AS Value
@user4676307, No No, just use a IN operator in my answer like CASE WHEN IFNULL(attributes.name,'') IN ('Product Type' ,'From actor')
|
0

Try:

SELECT DISTINCT
unspsc_commodity.`name` AS Commodity,
unspsc_code.`code` AS `Code`,
attributes.`value`
FROM
products
INNER JOIN unspsc_code ON products.unspsc_code_id =     unspsc_code.unspsc_code_id
INNER JOIN unspsc_commodity ON unspsc_code.unspsc_commodity_id =   unspsc_commodity.unspsc_commodity_id
INNER JOIN attributes ON products.product_id = attributes.product_id
WHERE 1

1 Comment

I can not remove "attributes.name = 'Product Type'" from where statement as attributes tables is has loads of other name and I only need to select where attribute.name is product type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.