gpt4 book ai didi

php - 在php中将3个表合并到一个选择查询中

转载 作者:行者123 更新时间:2023-11-29 19:53:21 25 4
gpt4 key购买 nike

如何在 php 中将这 3 个表合并到一个查询中?当我使用此查询时,我从 matches 和 sport 中获取的值为空。但是当我将最后一个右连接更改为左连接时,schedule 的值变为空。我怎样才能解决这个问题?谢谢。

PHP

<?php  
$db_name = "atfest_db";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";
$sql = "SELECT teamone,teamtwo,s_name,start,end,venue FROM `sport` right JOIN `matches` ON `matches`.`s_id` = `sport`.`s_id` right JOIN `schedule` ON `schedule`.`m_id` = `matches`.`m_id`";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);


$response = array();


while($row=mysqli_fetch_array($result))

{
array_push($response, array("teamone"=>$row[0], "teamtwo"=>$row[1],
"s_name"=>$row[2]));

}

echo json_encode (array("schedule_response"=>$response));




mysqli_close($con);
?>

体育表通过b s_id 连接到比赛表,而比赛表通过m_id 连接到赛程表。

匹配表 enter image description here

日程表 enter image description here

运动 table enter image description here

这是我得到的输出

{"schedule_response":[{"start":null,"end":null,"venue":null,"teamone":
"hehe","teamtwo":"haha","s_name":"Soccer"}

我需要的是开始、结束和地点从时间表表中获取值。像这样的事情

{"schedule_response":[{"start":2016-11-30 00:00:00,"end":2016-12-01   
00:00:00,"venue":aaaaaa,"teamone":"hehe","teamtwo":"haha","s_name":"Soccer"},

所有表的示例数据 enter image description here

最佳答案

INNER JOIN 运动和比赛然后 LEFT JOIN 时间表

Matches.m_id 表示 150 和 151,而 Schedule.m_id 表示 null 和 null。

尝试将schedule.m_id中的NULL和NULL替换为150和151,然后检查结果

关于php - 在php中将3个表合并到一个选择查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40762063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com