MYSQL SELECT和JSON_ENCODE

时间:2011-06-08 03:26:38

标签: php mysql json

我需要从mysql中选择json编码数组中的一个键。

SELECT * FROM json_field_table WHERE {var from JSON encoded array} = {expected value}

或者什么.. 我怎么能这样做?

PS:英语不好,我知道..

3 个答案:

答案 0 :(得分:3)

你必须使用子串匹配。 MySQL没有任何东西可以处理JSON数据,并像处理任何其他随机文本一样对待它。

SELECT ... WHERE the_json_field LIKE '%"var":"value"%';

答案 1 :(得分:0)

嗯,加布里埃尔,尽管问题的本质。我想,你的问题可能是,你需要读取一个JSON值,并根据这些值,你需要从表中检索记录集。如果是这种情况,这是您的解决方案。

// A sample json string
$json = '{ "field1": "value1", "field2": "value2" }'; 

// Here we will convert the json string into readable assocative array
$json_array = json_decode($json,true); 

//Next we will use it on a query
$query = "SELECT * json_field_table WHERE `".$json_array['field1']."` = 'Some Value' ";

//Execute the query
$result = mysql_query($query);

答案 2 :(得分:0)

使用数字(整数)可以过滤掉值,字母数字字符串更复杂,因为存储的值是“jsonencoded”

检查我对17955206

的回答