Mysql到Json。这可以用MySQL解决吗?

时间:2014-01-13 15:25:09

标签: php mysql sql json

我有一个MySQL查询,它将被转换为JSON并在Obj C中用于具有特定id的每个用户。我相信这是一个MySQL谜题,但JSON可能有一个答案。我不问很多问题,所以我会尽量简明扼要。

以下是一个用户的值的屏幕截图。如果您注意到,field_id可能会有所不同,因为并非所有信息都是必需的,因此对于给定的user_id,id字段将从3到8不等:

MySQL scenario screenshot

我必须查询结果GROUP BY column1(user_id),但只有WHERE column2(field_id)具有以下值:field_id ='18'和field ='19',那么(大问题)我需要使用column3(value)的值填充一个GROUP中的结果,这样我就可以在一个JSON对象中获得结果。

我已经知道如何转换为JSON以便在iOS中使用,但我只能将它作为两个对象提供给我。

我当前的查询

$query = "SELECT * FROM table1 WHERE field_id='18' OR field_id='19' ORDER BY user_id ";

当前结果

[{"id":"5","user_id":"461","field_id":"18","value":"1_MX4zNjcxNzM5Mn4x...","access":"0"},

{"id":"6","user_id":"461","field_id":"19","value":"T1==cGFydG5lcl9pZD0...","access":"0"},

{"id":"11","user_id":"463","field_id":"18","value":"1_MX4zNjcxNzM...","access":"0"},

{"id":"12","user_id":"463","field_id":"19","value":"T1==cGFydG5lcl9...","access":"0"}]

我需要两个匹配user_id字段的JSON对象作为一个对象,其结果将field ='18'值与field ='19'值区分开来。类似的东西:

    [{"id":"5","user_id":"461","field_id":"18","value18":"1_MX4zNjcxNzM5Mn4x...","value19":"T1==cGFydG5lcl9pZD0...","access":"0"},

{"id":"11","user_id":"463","field_id":"18","value18":"1_MX4zNjcxNzM...","value19":"T1==cGFydG5lcl9...","access":"0"}]  

OR

    [{"id":"5","user_id":"461","field_id='18'":"1_MX4zNjcxNzM5Mn4x...","field='19'":"T1==cGFydG5lcl9pZD0...","access":"0"},

{"id":"11","user_id":"463","field_id='18'":"1_MX4zNjcxNzM...","field='19'":"T1==cGFydG5lcl9...","access":"0"}]  

感谢...

2 个答案:

答案 0 :(得分:1)

我不能100%确定这是否有效。我不能轻易尝试它的JSON输出。但是,如果你运行这样的查询呢?

SELECT id, user_id,
GROUP_CONCAT(field_id,':',value) field_id,
access
FROM table1 WHERE field_id='18' OR field_id='19' 
GROUP BY user_id
ORDER BY user_id 

答案 1 :(得分:0)

在阅读结果集时,您可能希望在PHP中处理此问题。

因此请使用您当前的

查询
SELECT * FROM table1 WHERE field_id='18' OR field_id='19' ORDER BY user_id

但是构建结果数据对象如下:

$results = array();
$i = -1;
$current_user_id = '';
while ($row = [YOUR MySQL FETCH MECHANISM HERE]) {
    // determine if this record represents a new user id in the result set
    // if so, we need to set up the user object and start another entry in top level array  
    if($current_user_id != $row['user_id']) {
        // you have moved to a new user in the result set
        // increment your array counter and set current user id
        $i++; // will set value to 0 on first iteration
        $current_user_id = $row['user_id'];
        // build new array entry
        $results[$i] = new stdClass();
        $results[$i]->user_id = $current_user_id;
        $results[$i]->fields = array(); 
    }
    // build field object for insertion
    $field = new stdClass();
    $field->field_id = $row['field_id'];
    $field->value = $row['value'];
    $results[$i]->fields[] = $field;
}

在编码$results时,这会给你一个像这样的JSON结构:

[
  {
    "user_id":"461",
    "fields": [
      {
        "field_id":"18",
        "value":"foobar"
      },
      {
        "field_id":"19",
        "value":"abcxyz"
      }
    ]
  },
  ...
]

这种数据结构比使用爆炸连接字段id /值字符串的某些解决方案更容易使用app。另请注意,我没有在数据结构中的任何位置包含id字段,因为它在此上下文中没有任何意义。如果您确实需要该ID,则可以将其添加为field对象中的另一个属性,因为这是一对一关系(而不是user_id)。

根据您的评论,如果字段值已知并且您需要通过field_id索引访问它,您可以稍微修改我上面显示的内容以构建可索引的字段列表而不是简单的对象数组:

$results = array();
$i = -1;
$current_user_id = '';
while ($row = [YOUR MySQL FETCH MECHANISM HERE]) {
    // determine if this record represents a new user id in the result set
    // if so, we need to set up the user object and start another entry in top level array  
    if($current_user_id != $row['user_id']) {
        // you have moved to a new user in the result set
        // increment your array counter and set current user id
        $i++; // will set value to 0 on first iteration
        $current_user_id = $row['user_id'];
        // build new array entry
        $results[$i] = new stdClass();
        $results[$i]->user_id = $current_user_id;
        $results[$i]->fields = array(); 
    }
    // insert field value at field_id index position
    $results[$i]->fields[$row['field_id']] = $row['value'];
}

这会给你一个像这样的JSON表示。

[
  {
    "user_id":"461",
    "fields": {
        "18":"foobar",
        "19":"abcxyz"
    }
  },
  ...
]

这样可以让客户根据字段ID轻松查找。当然,您可能需要考虑类似user_id。从用户对象中,您可以访问fields->18(或类似的基于客户端语言语法)。