Fetch different columns conditionally using MySQL CASE with 2 tables
SELECT C.id,
(CASE
WHEN C.size= 1 THEN P.`price0-3`
WHEN C.size = 2 THEN P.`price3-6`
WHEN C.size = 3 THEN P.`price6-9`
WHEN C.size = 4 THEN P.`price9-12`
END)
FROM cart AS C
LEFT JOIN products AS P ON P.unique_id = C.fk_products
WHERE C.mac_address ="74be16979710d4c4e7c6647856088456"
Image1
Image2
Image3
-------------------------
SELECT
(CASE
WHEN C.size= 1 THEN P.`price0-3`
WHEN C.size = 2 THEN P.`price3-6`
WHEN C.size = 3 THEN P.`price6-9`
WHEN C.size = 4 THEN P.`price9-12`
END) As totalA
FROM cart AS C
LEFT JOIN products AS P ON P.unique_id = C.fk_products
WHERE C.mac_address ="74be16979710d4c4e7c6647856088456"
-------------------------------
SELECT
SUM((CASE
WHEN C.size= 1 THEN P.`price0-3`
WHEN C.size = 2 THEN P.`price3-6`
WHEN C.size = 3 THEN P.`price6-9`
WHEN C.size = 4 THEN P.`price9-12`
END)) As totalA
FROM cart AS C
LEFT JOIN products AS P ON P.unique_id = C.fk_products
WHERE C.mac_address ="74be16979710d4c4e7c6647856088456"
Share on Facebook
Page views:179