Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多个查询合并为一个查询,并使用单个while循环获取数据?_Php_Mysql_Json_Database_Pdo - Fatal编程技术网

Php 如何将多个查询合并为一个查询,并使用单个while循环获取数据?

Php 如何将多个查询合并为一个查询,并使用单个while循环获取数据?,php,mysql,json,database,pdo,Php,Mysql,Json,Database,Pdo,我有这段代码,如何将多个查询及其结果组合成一个 <?php $obj = new stdClass(); include('pdoConfig.php'); $response = array(); $sql1 = $dbh->prepare("select * from orders_assigned where delivery_status != 'Cancelled' && order_status='Picked'"); $sql1->exec

我有这段代码,如何将多个查询及其结果组合成一个

<?php
$obj = new stdClass();
include('pdoConfig.php');
$response = array();
$sql1     = $dbh->prepare("select * from orders_assigned where delivery_status != 'Cancelled' && order_status='Picked'");
$sql1->execute();
$count1 = $sql1->rowCount();
if ($count1 >= 1) {
    while ($row1 = $sql1->fetch()) {
        $delivery_status = $row1['delivery_status'];
        $deliveryboy_id  = $row1['username'];
        $order_id        = $row1['order_id'];
        $sql2            = $dbh->prepare("select * from delboy_login where id = ?");
        $sql2->bindParam(1, $deliveryboy_id);
        $sql2->execute();
        $row2      = $sql2->fetch();
        $del_name  = $row2['name'];//name
        $del_lat   = $row2['lat'];//lat
        $del_longi = $row2['longi'];//long
        $del_icon  = $row2['icon'];//icon
        $sql3      = $dbh->prepare("select * from `order` where `order_id` = ?");
        $sql3->bindParam(1, $order_id);
        $sql3->execute();
        $row3       = $sql3->fetch();
        $address_id = $row3['address_id'];
        $user_id    = $row3['user_id'];
        $sql4       = $dbh->prepare("select * from customer_login where cust_id = ?");
        $sql4->bindParam(1, $user_id);
        $sql4->execute();
        $row4 = $sql4->fetch();
        $cus_name  = $row4['name'];//name
        $sql5 = $dbh->prepare("select * from address where a_id = ?");
        $sql5->bindParam(1, $address_id);
        $sql5->execute();
        $row5      = $sql5->fetch();
        $cus_lat   = $row5['lat'];//lat
        $cus_longi = $row5['longi'];//long
        $cus_icon  = $row5['icon'];//icon

        $tmp            = array();
        $tmp['lat']     = $del_lat;//i want use $cus_lat here too
        $tmp['content'] = $del_name;//i want use $cus_name here too
        $tmp['lng']     = $del_longi;//i want use $cus_longi here too
        $tmp['icon']    = $del_icon;//i want use $cus_icon here too
        array_push($response, $tmp);
    }
}
echo json_encode($response, JSON_NUMERIC_CHECK);
等等

select orders_assigned.*, order.*, address.* 
from orders_assigned
inner join delboy_login on delboy_login.id = orders_assigned.delboy_login
inner join `order` on `order`.`id` = orders_assigned.order_id
inner join customer_login on customer_login.cust_id = order.user_id
inner join address on address.a_id = order.address_id
where delivery_status != 'Cancelled' && order_status='Picked'
我想要这样的最终输出

{
"icon" : "icon path here",
"del_name" : "Boy One",
"del_lat" : 26.8808383,
"del_longi" : 75.7503407,
},
{
"icon" : "icon path here",
"cus_name" : "Roylee Wheels",
"cus_lat" : 20.594725,
"cus_longi" : 78.963407
},
等等

select orders_assigned.*, order.*, address.* 
from orders_assigned
inner join delboy_login on delboy_login.id = orders_assigned.delboy_login
inner join `order` on `order`.`id` = orders_assigned.order_id
inner join customer_login on customer_login.cust_id = order.user_id
inner join address on address.a_id = order.address_id
where delivery_status != 'Cancelled' && order_status='Picked'
你需要加入你的表格。这是未经测试的,所以是这样的

从表中选择*是不好的做法,实际上应该只定义所需的字段

还要注意的是,order在mysql中是一个保留字,所以对于表来说不是一个好名字——因此使用backticks

SELECT 
    orders_assigned.delivery_status AS delivery_status,
    orders_assigned.username AS username,
    orders_assigned.order_id AS order_id,
    delboy_login.name AS del_name,
    delboy_login.lat AS del_lat,
    delboy_login.longi AS del_longi,
    order.address_id AS address_id,
    order.user_id AS user_id,
    customer_login.name AS cus_name,
    address.lat AS cus_lat,
    address.longi AS cus_longi
FROM
    orders_assigned
        LEFT JOIN
    delboy_login ON delboy_login.id = orders_assigned.username
        LEFT JOIN
    orders ON order.order_id = orders_assigned.order_id
        LEFT JOIN
    customer_login ON customer_login.cust_id = order.user_id
        LEFT JOIN
    address ON address.a_id = order.address_id
这是您想要的SQL

请注意,您可以重命名查询返回的列,如下所示

    delboy_login.name AS del_name,
    delboy_login.lat AS del_lat,
    delboy_login.longi AS del_longi,

    customer_login.name AS cus_name,
    address.lat AS cus_lat,
    address.longi AS cus_longi
连接表是非常重要的,我希望您将来能够选择查询模式

祝你好运

编辑:

只需创建两个不同的temp_阵列,并将它们一个接一个地推入主阵列,即可实现所需的输出

if ($count1 >= 1) {
    while ($row = $sql->fetch()) {
        $delivery_status = $row['delivery_status'];
        $deliveryboy_id  = $row['username'];
        $order_id        = $row['order_id'];
        $del_name  = $row['del_name'];//name
        $del_lat   = $row['del_lat'];//lat
        $del_longi = $row['del_longi'];//long
        $del_icon  = $row['del_icon'];//icon
        $address_id = $row['address_id'];
        $user_id    = $row['user_id'];
        $cus_name  = $row['cus_name'];//name
        $cus_lat   = $row['cus_lat'];//lat
        $cus_longi = $row['cus_longi'];//long
        $cus_icon  = $row['cus_icon'];//icon

        $tmp_del                 = array();
        $tmp_del['del_lat']      = $del_lat;
        $tmp_del['del_name']     = $del_name;
        $tmp_del['del_longi']    = $del_longi;
        $tmp_del['del_icon'] = $del_icon;

        $tmp_cus               = array();
        $tmp_cus['cus_lat']    = $cus_lat;
        $tmp_cus['cus_name']   = $cus_name;
        $tmp_cus['cus_longi']  = $cus_longi;
        $tmp_cus['cus_icon']   = $cus_icon;

        array_push($response, $tmp_del);
        array_push($response, $tmp_cus);
    }

试试这个:它会有用的

SELECT oa.delivery_status, oa.username oa.order_id, dl.name, dl.lat, dl.longi, o.address_id, o,user_id, cl.name, a.lat, a.longi
FROM orders_assigned as oa 
JOIN delboy_login as dl ON oa.username = dl.id 
JOIN order as o ON oa.order_id = o.order_id
JOIN customer_login as cl ON  = o.user_id = cl.cust_id
JOIN address as a ON  = o.address_id = a.a_id
WHERE oa.delivery_status != 'Cancelled' && oa.order_status='Picked'

“因为我不擅长加入”这不是一个更好的机会吗?还有什么比这更好的激励呢?我用来训练我的SQL技能的是使用HeidiSQL软件(它是免费的)。在该软件中,您可以登录数据库并对其进行管理。此外,您还可以进行查询,以培训您的技能。(然而这是个人的)我想要这样的最终输出在某些情况下有效,但在您的查询中,所有结果合并为一个,但我想要像del_name和cus_name一样获取其他名称,我使用这段代码的目的是,我使用这些数据在谷歌地图上放置标记,所以我希望单独的客户名称和del_名称作为单独的标记,其长度在单个查询中。我得到以下信息:-“交货状态”:交货,“用户名”:1,“订单id”:5,“del_名称”:男孩一,“del_lat”:26.8808383,“del_longi”:75.7503407,“地址id”:31,“用户id”:1,“用户姓名”:Roylee Wheels,“用户姓名”:20.593684,“用户姓名”:78.96288我想要{“用户姓名”:男孩一,“用户姓名”:26.8808383,“用户姓名”:75.7503407,},{“用户姓名”:Roylee Wheels,“用户姓名”:20.593684,“用户姓名”:78.96288}@Anilbairva刚刚更新了答案。看看这是不是你想要的。