如何从存储在序列化JSON列中的Mysql列中提取数据?

时间:2011-02-23 17:14:59

标签: mysql serialization

我在mysql DB中有一个包含序列化数据的列(使用PHP JSON编码来序列化它们)。

以下是一个例子:

{“name”:“Group ltd”,“email”:“support@domain.org”,“auth”:“Andrey Ucholnik”}

mysql中是否有内置函数在没有PHP的情况下提取这些值? 我的意思是构建将反序列化数据的查询。

当然可以使用LOCATE和SUBSTR功能组合来做到这一点,但我更喜欢内置的东西。

3 个答案:

答案 0 :(得分:1)

在MySQL中没有内置函数,但是在PHP中使用小代码可以很容易地在下面执行:

<?php
    $json = '{{"generated": "2010-02-26T22:26:03.156866   blahblahblah   ": null, "thumbnail_url": "http://thumbs.mochiads.com/c/g/tetword-pro/_thumb_100x100.jpg", "screen4_url": "http://thumbs.mochiads.com/c/g/tetword-pro/screen4.png", "leaderboard_enabled": true, "resolution": "600x550", "width": 600}]}}';
    $out = json_decode($json, true);
    foreach($out["games"] as $game) {
        $name = addslashes($game[name]);
        $description = addslashes($game[description]);
        //Here you can use your mysql_insert code
        mysql_query("INSERT INTO games (name, description) VALUES('$name', '$description')") or die (mysql_error());
    } 
?>

答案 1 :(得分:0)

没有内置的MySQL函数可以使用JSON,但这是一个非常简单的存储函数,用于从JSON中提取值:

DELIMITER $$
CREATE FUNCTION JSON_EXTRACT(json TEXT, name CHAR(64))
RETURNS CHAR(64) DETERMINISTIC
BEGIN
    SET @namePos = LOCATE(name, json);
    IF @namePos = 0 THEN RETURN ''; END IF;
    SET @valuePos = LOCATE(':', json, @namePos) + 1;
    IF SUBSTR(json, @valuePos, 1) = '"' THEN
        SET @valuePos = @valuePos + 1;
        RETURN SUBSTR(json, @valuePos, LOCATE('"', json, @valuePos) - @valuePos);
    ELSE
        SET @valueBegin = TRIM(SUBSTR(json, @valuePos));
        SET @delim1 = LOCATE(' ', @valueBegin); SET @delim1 = IF(@delim1 = 0, 64, @delim1);
        SET @delim2 = LOCATE(',', @valueBegin); SET @delim2 = IF(@delim2 = 0, 64, @delim2);
        SET @delim3 = LOCATE('}', @valueBegin); SET @delim3 = IF(@delim3 = 0, 64, @delim3);
        RETURN LEFT(@valueBegin, LEAST(@delim1, @delim2, @delim3) - 1);
    END IF;
END$$

用法示例:

SELECT JSON_EXTRACT('{"a":"aa","b" : 1, "c": 3}', 'b') AS test;

请注意,该功能有许多限制。例如,它不处理嵌套类,并且键值不应包含在值中。

答案 2 :(得分:0)

是的,您绝对可以使用mysql中的JSON_EXTRACT()函数。

让我们获取一个包含JSON的表(此处为表client_services):

+-----+-----------+--------------------------------------+
| id  | client_id | service_values                       |
+-----+-----------+------------+-------------------------+
| 100 |      1000 | { "quota": 1,"data_transfer":160000} |
| 101 |      1000 | { "quota": 2,"data_transfer":800000} |
| 102 |      1000 | { "quota": 3,"data_transfer":70000}  |
| 103 |      1001 | { "quota": 1,"data_transfer":97000}  |
| 104 |      1001 | { "quota": 2,"data_transfer":1760}   |
| 105 |      1002 | { "quota": 2,"data_transfer":1060}   |
+-----+-----------+--------------------------------------+

要选择每个JSON字段,请运行以下查询:

SELECT 
    id, client_id, 
    json_extract(service_values, '$.quota') AS quota,
    json_extract(service_values, '$.data_transfer') AS data_transfer
FROM client_services;

所以输出将是:

+-----+-----------+----------------------+
| id  | client_id | quota | data_transfer|
+-----+-----------+----------------------+
| 100 |      1000 |     1 |       160000 |
| 101 |      1000 |     2 |       800000 |
| 102 |      1000 |     3 |        70000 |
| 103 |      1001 |     1 |        97000 |
| 104 |      1001 |     2 |         1760 |
| 105 |      1002 |     2 |         1060 |
+-----+-----------+----------------------+

希望这会有所帮助!