gpt4 book ai didi

php - 如何在 PHP 代码中将两个表中的两个查询合并为一个查询?

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

我开始学习 php,我编写了 2 个查询来从两个表中获取数据。

目前,这些是我的表:

食品供应商

 ID     Name         Description  
-----------------------------
1 Vendor 1 testing
2 Vendor 2 testing
3 Vendor 3 testing

food_vendor_menu

 ID     VENDOR_ID   FOOD_NAME  
-----------------------------
1 1 Food 1
2 1 Food 2
3 2 Food 3
4 2 Food 4
5 3 Food 5

food_vendor_menu 中的每个 vendor_id 对应于 food_vendors 中的 id。因此,供应商 1 和供应商 2 都有 2 种食品,而供应商 3 只有 1 种食品。

目前,我正在执行 2 个查询并像这样遍历它们:

$sql = "select * from food_vendors";

$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$jsonData = array();
$rowCount = $result->num_rows;
$index = 1;
while($row =mysqli_fetch_assoc($result))
{
$sqlnew = "select * from food_vendor_menu where vendor_id=" .$row['id']. "" ;

$resultnew = mysqli_query($connection, $sqlnew) or die("Error in Selecting " . mysqli_error($connection));

$jsonData = array();
$rowCountnew = $resultnew->num_rows;
$indexnew = 1;
$menuStrings = array();
if ($rowCountnew >0)
{
while($rownew =mysqli_fetch_assoc($resultnew))
{
$menuStrings[$indexnew] = array("id" => $rownew['id'], "food_name" => $rownew['food_name']);

++$indexnew;
}
}

echo '"item'.$index.'":';
echo json_encode(array("id" => intval($row['id']), "name" => $row['name'], "description" => $row['description'], "menu_items" =>$menuStrings));
if ($rowCount != $index)
{
echo ',';
}
++$index;
}

echo ' }';

这会产生以下输出:

{
"item1": {
"id": 1,
"name": "Vendor 1",
"description": "testing":,
"menu_items": {
"1": {
"id": "1",
"food_name": "Food 1"
},
"2": {
"id": "2",
"food_name": "Food 2"
}
}
},
"item2": {
"id": 2,
"name": "Vendor 2",
"description": "testing"
"menu_items": {
"1": {
"id": "3",
"food_name": "Food 3"
},
"2": {
"id": "4",
"food_name": "Food 4"
}
}
},
"item3": {
"id": 3,
"name": "Vendor",
"description": "testing",
"menu_items": {
"1": {
"id": "5",
"food_name": "Food 5",
}
}
}
}

但是,我不认为这是最好的方法,我喜欢只使用一个查询来产生相同的输出,所以经过研究,我尝试学习并将上面的代码替换为以下,将 2 个表连接在一起:

$sql = "select * from food_vendor_menu s join food_vendors t on t.id=vendor_id" ;
$q = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$jsonData = array();
while($row = mysqli_fetch_assoc($q))
{
if (isset( $row['vendor_id'] ) )
{
$index = 'item'.$row['vendor_id'].'';
$jsonData[$index] = $row;
unset( $jsonData[$index]['food_name'] );
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];
}
}

echo json_encode($jsonData);

输出结果:

{
"item1": {
"id": "1",
"vendor_id": "1",
"name": "Vendor 1",
"description": "testing",
"menu_items": "Food 1"
},
"item2": {
"id": "2",
"vendor_id": "2",
"name": "Vendor 2",
"description": "testing",
"menu_items": "Food 3"
},
"item3": {
"id": "3",
"vendor_id": "3",
"name": "Vendor 3",
"description": "testing",
"menu_items": "Food 5"
}
}

我知道这是更好的方法并且更有效,但是正如您从上面的输出中看到的那样,Vendor 1 的 menu_items 数组只有 Food 1,但是没有 食物 2。其他 2 个供应商也有同样的问题。此外,我需要将 vendor_id 附加到每个菜单项,而不仅仅是食物名称。

我知道问题出在哪里,只是不知道如何解决,因为我目前对 php 的了解有限。

有人可以帮忙吗?

更新:

按照 Joey 的建议,我更换了:

$jsonData[$index] = $row
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];

与:

if ( !isset( $jsonData[$index] ) )
{
$jsonData[$index] = $row;
$jsonData[$index]['menu_items'] = array();
}
array_push($jsonData[$index]['menu_items'], $row['food_name']);

我得到以下输出:

"item1": {
"id": "1",
"name": "Vendor 1",
"description": "testing",
"menu_items": ["Food 1", "Food 2"]
}

但是,我需要让每个 Food 1Food 2 本身成为一个数组,因为我需要将其他数据关联到每个食品,例如 id 、食品价格等

最佳答案

$sql = "select * from food_vendor_menu s join food_vendors t on t.id=vendor_id";
...
$jsonData[$index] = $row
...
$jsonData[$index]['menu_items'] = array();
$jsonData[$index]['menu_items'] = $row['food_name'];

应该是这样的:

$sql = "select s.id as food_id, s.food_name, s.vendor_id, t.name, t.description from food_vendor_menu s join food_vendors t on t.id=vendor_id";
...
if (!isset($jsonData[$index])) {
$jsonData[$index] = $row;
unset( $jsonData[$index]['food_id'], $jsonData[$index]['food_name'] );
$jsonData[$index]['menu_items'] = array();
}
array_push($jsonData[$index]['menu_items'],
array('id'=>$row['food_id'], 'food_name'=>$row['food_name']));

关于php - 如何在 PHP 代码中将两个表中的两个查询合并为一个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412134/

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