2014-02-10 26 views
0

我有mysql列:mysql |具體要求

[{"name":"Color","value":"Red (+5$)","price":"+5"}] // options column 

,並要求:

SELECT C.options, C.qty, P.PRICE 
FROM `cart` C 
LEFT JOIN `products` P ON C.productid = P.id 
WHERE C.userid = '3c9494e7ff22e2a7ac01a3e95fbbc0e4' 

但我需要這樣的:

SELECT C.options, C.qty, (P.PRICE + (substract price +5 from options column))*C.qty 
FROM `cart` C 
LEFT JOIN `products` P ON C.productid = P.id 
WHERE C.userid = '3c9494e7ff22e2a7ac01a3e95fbbc0e4' 

是否有可能獲得上述要求? 謝謝。

+1

爲什麼?爲什麼要在DBMS中解析或使用JSON做些事情?如果這是您的應用程序的意圖 - 然後將數據存儲在單獨的列 –

回答

1

我認爲要彙總值

試試這個

SELECT C.options, C.qty, SUM(P.PRICE + c.options) * C.qty 
FROM `cart` C 
LEFT JOIN `products` P ON C.productid = P.id 
WHERE C.userid = '3c9494e7ff22e2a7ac01a3e95fbbc0e4' 
GROUP BY c.options,c.qty 

如果您需要將選項列轉換,做這樣的

SELECT C.options, C.qty, SUM(P.PRICE + CAST(c.options AS DECIMAL)) * C.qty 
FROM `cart` C 
LEFT JOIN `products` P ON C.productid = P.id 
WHERE C.userid = '3c9494e7ff22e2a7ac01a3e95fbbc0e4' 
GROUP BY c.options,c.qty 
+0

我不能使用c.options,因爲這是一個字符串。 – user889349

+0

好的!但是,這是你想在這裏使用的列嗎? –