2014-02-10 27 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

好的!但是,这是你想在这里使用的列吗? –