I have a query where i need to select product options from multi tables.
SELECT
p.option_id,
p.product_id,
p.sku,
p.qty,
p.price,
c.color_label,
s.size_label,
m.material_label,
v.variation_label
FROM product_options p
LEFT JOIN option_color c ON c.color_id = p.color_id
LEFT JOIN option_size s ON s.size_id = p.size_id
LEFT JOIN option_material m ON m.material_id = p.material_id
LEFT JOIN option_variation v ON v.variation_id = p.variation_id
WHERE p.product_id = 1 ;
This works fine. However; i get NULL values Like so
Business rule: as follows if any of the columns contains NULL means the whole product variation (column) of that particular option will be NULL.
MY QUESTION
How i can dynamically remove any of the columns once there result contain NULL.
Example from the above pic.
material_label = NULL
then the whole column should be removed.
I hope i explained it well.
EDIT
Just adding further explaanation.
What im trying to achive is as follow
- Get specific product from the DB with its variants(try and minimize non required data).
- List item then do some array cleaning and grouping then maybe convert it to JSON using PHP.
- then using javascript to get user selection on the product.
-
Firstly - use INNER JOIN instead of LEFT (study the difference carefully !!!). Then, if some column in some options table may contain NULL, add according WHERE condition.Akina– Akina2022年05月19日 04:38:26 +00:00Commented May 19, 2022 at 4:38
-
There are two ways to get NULL -- The JOIN may not find the matching row in the righthand table, or the matching row of the righthand table exists but contains NULL. Which case(s) are you concerned with? (I think the solution depends on your answer.)Rick James– Rick James2022年05月22日 04:10:40 +00:00Commented May 22, 2022 at 4:10
-
Thanks @Akina i think im over thinking it. My current solutions is the common way of handing such a case. i need to find a way through programming language im using to enhance my results. from the data you can tell that its products variants. once user select color then system should change selection based on the available sizes of that color. nevertheless. i think my solution need a combination of the levels, First get data from DB exactly as i did above then PHP to make data in a json Maybe and leave the rest for Javascript to handleAmQ7– AmQ72022年05月22日 12:09:44 +00:00Commented May 22, 2022 at 12:09
-
Thanks @RickJames Please read above comment.AmQ7– AmQ72022年05月22日 12:10:44 +00:00Commented May 22, 2022 at 12:10
-
@AmQ7 - "will be NULL" -- I read that to mean that you want to change the column to say NULL in each row. Am I misunderstanding you? Please provide an example for each case, and show both input and output. Perhaps simplify the table to only 2 label columns? Or can you show all cases by using different columns?Rick James– Rick James2022年05月22日 15:54:43 +00:00Commented May 22, 2022 at 15:54
2 Answers 2
it's better to use INNER JOIN
instead LEFT JOIN
, but if you don't want to change your query, you can add this at the end of your query:
WHERE p.product_id = 1
AND m.material_label IS NOT NULL
AND v.variation_label IS NOT NULL;
-
thanks @YousefRahimy . using your answer i will lose the whole row if it contains Null. I regrads to Inner Join. Could you provide me with a sample. thanksAmQ7– AmQ72022年05月23日 21:54:44 +00:00Commented May 23, 2022 at 21:54
You should change the SELECT clause altogether
For example, combine the output of m.material_label and v.variation_label
CONCAT(
IFNULL(m.material_label,'')
,IF(IFNULL(m.material_label,'')='','',':')
,IFNULL(v.variation_label,'')
) material_variation
This will return an empty string if m.material_label
and v.variation_label
are NULL
.
If you would like to combine all the columns so that material_variation is completely gone, you can try something like this:
SELECT
CONCAT(
'option_id : ',p.option_id
,',product_id : ',p.product_id
,',sku : ',p.sku
,',qty : ',p.qty
,',price : ',p.price
,',color_label : ',c.color_label
,',size_label : ',s.size_label
,IF(IFNULL(m.material_label,'')='','',CONCAT(',material_label:',m.material_label))
,IF(IFNULL(v.variation_label,'')='','',CONCAT(',variation_label:',v.variation_label))
)
FROM product_options p
LEFT JOIN option_color c ON c.color_id = p.color_id
LEFT JOIN option_size s ON s.size_id = p].size_id
LEFT JOIN option_material m ON m.material_id = p.material_id
LEFT JOIN option_variation v ON v.variation_id = p.variation_id
WHERE p.product_id = 1 ;
This will return the entire row as key/value pairs
- If
m.material_label
isNULL
, it displays an empty string. Otherwise, the key/value pair. - If
v.variation_label
isNULL
, it displays an empty string. Otherwise, the key/value pair.