简体   繁体   English

无法合并三个 select 语句的结果

[英]Not able to merge results from the three select statements

Hey look here i am trying to get result from these three select statements at once but i am noit able to do so.嘿,看这里,我试图立即从这三个 select 语句中获得结果,但我无法做到。 So please help me in rectifying my mistake.所以请帮助我纠正我的错误。 Don't rate me negative if you found my english or question method bad.如果您发现我的英语或提问方法不好,请不要给我负面评价。

 $sql = $conn->prepare("SELECT Count(c.c_id) from complaints c, users u,cell_num cn where c.status=? AND c.u_id_fk=u.u_id AND u.u_id=cn.u_id_fk");
 $sql->bind_param("i",$statOpen);

 $sql .= $conn->prepare("SELECT Count(c.c_id) from complaints c, users u,cell_num cn where c.status=? AND c.u_id_fk=u.u_id AND u.u_id=cn.u_id_fk");
 sql->bind_param("i",$statProgress);

 $sql .= $conn->prepare("SELECT Count(c.c_id) from complaints c, users u,cell_num cn where c.status=? AND c.u_id_fk=u.u_id AND u.u_id=cn.u_id_fk");
 $sql->bind_param("i",$statClosed);

                        $sql->execute();

                        $sql->store_result();

                        if($sql->num_rows > 0)
                        {
                            $sql->bind_result($c_id);

                            while( $sql->fetch() )
                            {
                                $user[] = array(
                               'c_id'=>$c_id


                             );
                            }
                            echo json_encode($user);


                            $sql->close();
                        }

You can do conditional aggregation:您可以进行条件聚合:

select 
    sum(c.status = ?) cnt_open,
    sum(c.status = ?) cnt_progress,
    sum(c.status = ?) cnt_closed
from complaints c
inner join users u on u.u_id = c.u_id_fk
inner join cell_num cn on cn.u_id_fk = u.u_id

This query accepts three parameters at once, that correspond to the three values that you were passing to your three individual queries.此查询一次接受三个参数,它们对应于您传递给三个单独查询的三个值。

Note that I modified the query to use standard joins (with the on keyword) rather than old-school, implicit joins (with commas in the from clause): this archaic syntax should not be used in new code.请注意,我修改了查询以使用标准连接(使用on关键字)而不是老式的隐式连接(在from子句中使用逗号):这种古老的语法不应该在新代码中使用。

You can make the query more efficient by adding a where clause that filters on the three possible status es (this requires passing each parameter twice):您可以通过添加一个过滤三个可能statuswhere子句来提高查询效率(这需要将每个参数传递两次):

where c.status in (?, ?, ?)

you can do so, it's more optimised你可以这样做,它更优化

select c.statut,count(c.c_id)
from complaints c
inner join users u on u.u_id = c.u_id_fk
inner join cell_num cn on cn.u_id_fk = u.u_id
where c.status in (?, ?, ?)
group by c.status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM