【问题标题】:How to do Inner Join with multiple rows return JSON in PHP如何在 PHP 中使用多行返回 JSON
【发布时间】:2015-09-29 23:30:32
【问题描述】:

我有两张表,其中一张表有多行参考另一张表。

问题表:

id | value_question
---------
1  | "first question"
2  | "second question"

responses_table:

id | question_id| value_response | is_true
-------------------------------------------
1  | 1          | SFU            | true
2  | 1          | UBC            | false
3  | 2          | BU             | true
4  | 2          | RI             | false

我会知道从 PHP 的 JSON 中的 questions_table 中返回每一行的最佳做法是:

[
  { "value_question": "first question",
    "responses": [
      {"value_response": "SFU", "is_true": "true"},
      {"value_response": "UBC", "is_true": "false"}
    ],
  },
  { "value_question": "first question",
    "responses": [
      {"value_response": "SFU", "is_true": "true"},
      {"value_response": "UBC", "is_true": "false"}
    ],
  },
]
 

我试图仅在 MYSQL 中执行此操作,但我只能对字符串进行排序。

SELECT 
     value_question,
     CONCAT('[',GROUP_CONCAT('{value_response:',responses.value_response,', is_true:',responses.is_true,'}'),']')'responses' 
FROM questions
INNER JOIN responses ON 
     questions.id = responses.question_id
GROUP BY id_question

我不知道在 PHP 和 MYSQL 中执行此操作哪个更好。 如果您需要更多详细信息,请告诉我。

谢谢,我知道如何使用 json_encode,我的问题更多:这是拥有所描述结构的最佳请求。

php:

    include 'connect_db.php';

    $sql= *the request*

    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $json = json_encode($result);

    echo $json;

【问题讨论】:

  • 最好的方法是首先从更简单的查询中获取结果行,然后使用 PHP 和 json_encode() 生成您想要获得的有组织的结构。请发布您用于查询和获取的 PHP 代码,以便我们了解您使用的 MySQL API(MySQLi 与 PDO)并推荐最佳方法。您应该尝试在 MySQL 查询中构造 JSON 字符串。
  • 你正试图在 SQL 中做一些 SQL 并不真正适合的事情。像这样尝试将它们连接在一起是痛苦的并且容易出错。迈克尔的建议是最好的。
  • 我用 PHP 编辑过。我知道作为字符串的 concat 不是执行此类操作的好方法,但它是与我找到的目标更相似的解决方案。
  • 正如 Michael Berkowski 建议的那样,最好的方法是 PHP 的 json_encode,但是您需要以这种格式输出,例如在响应中,您需要在单行中使用多个结果行,特别是您的要求,制作 JSON使用 GROUP_CONCAT 输出效果最好,您可以创建一个视图并直接使用它的输出
  • 从 questions_table 中选择 qs.value_question、rs.value_response、rs.is_true 作为 qs INNER JOINresponses_table 作为 rs ON qs.id=rs.question_id ;运行此 sql,它将为您提供来自 mysql 的数据,如下所示 {"value_response": "SFU", "is_true": "true"}, {"value_response": "UBC", "is_true": "false"} 然后你可以使用 php 处理这个结果集。

标签: php mysql json


【解决方案1】:

请使用以下代码。
对于数据库连接,请在 mysqli_connect 函数中替换为您的 mysql 数据库服务器值。
我使用过 mysql 查询和 php mysqli,因为我不太了解 PDO。 首先在变量中获取请求,然后运行 ​​mysql 连接查询。遍历 mysql 获取的结果并建立一个数组。然后最后做json_encode。

$con = mysqli_connect('db_host', 'db_username', 'db_password', 'db_name'); // please replace with your mysql server values.

$value_question = 'first question'; // your *the request*

$value_question = $con->real_escape_string($value_question); // escape values

$query =  " SELECT `responses_table`.`value_response` , `responses_table`.`is_true` 
            FROM `questions_table` 
            INNER JOIN `responses_table` 
            ON `questions_table`.`id` = `responses_table`.`question_id` 
            WHERE `questions_table`.`value_question` = '$value_question' "; 

$result = $con->query($query);

while( $row = $result->fetch_assoc() ){ // build up a responses array
    $responses[] = array( 'value_response' => $row['value_response'] , 
                            'is_true'       => $row['is_true'])  ;
}
$response = array('value_question' => $value_question ,
                    'responses' => $responses );

echo json_encode($response); // your json response

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多