MySQL从json中仅选择不是0.00的值

时间:2017-05-15 17:00:06

标签: mysql json

我有桌价:

+----+----------+-----------------------------------------------------------
| id | reseller | price                                                    |                                              
+----+----------+-----------------------------------------------------------
| 20 |        1 | {"1": "4.00", "2": "5.00", "3": "3.00", "4": "2.00", "5": "1.00", "18": "0.00", "28": "0.00"} |
| 21 |        6 | {"1": "0.00", "2": "0.00", "3": "0.00", "4": "0.00", "5": "0.00", "18": "1.00", "28": "0.00"} |
| 22 |        2 | {"1": "0.00", "2": "0.00", "3": "0.00", "4": "0.00", "5": "0.00", "18": "0.00", "28": "0.00"} |
+----+----------+-----------------------------------------------------------------------------------------------+

并且只需要获得不是0.00的值(以ma形式显示只有定义价格的值)...所以我需要从输出中得到这个:

 +----+----------+-----------------------------------------------------------
| id | reseller | price                                                     |                                              
+----+----------+------------------------------------------------------------
| 20 |        1 | {"1": "4.00", "2": "5.00", "3": "3.00", "4": "2.00", "5": "1.00"}
-----------------------------------------------------------------------------

我只尝试使用它:

SELECT id, JSON_EXTRACT(price, '$.1') FROM prices WHERE reseller=1;

尝试获得第一个索引值女巫是4.00但我得到错误:

ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 3.

What sql query i need to do to show only values that have defined price and value is not 0.00?

1 个答案:

答案 0 :(得分:0)

使用JSON_EXTRACT(price, '$."1"')并且您可以在没有错误的情况下运行该查询,但我对Sloan,我不确定您是否只能在单个简单查询中选择json值与“0.00”不同。 这是您修改的查询:

SELECT id, reseller, JSON_EXTRACT(price, '$."1"') AS price FROM prices WHERE reseller=1;
+----+----------+---------------------------
| id | reseller | price                    |                                              
+----+----------+---------------------------
| 20 |        1 | "4.00"                   |
--------------------------------------------