简体   繁体   English

PHP/MYSQL 查询被另一个数组分组

[英]PHP/MYSQL Query to be grouped by another array

My today's need, and I hope you will find the great solution, is the following.我今天的需求,我希望你能找到很好的解决方案,如下。 I have a DB table with tasks, each of them with a status "open" or "closed".我有一个包含任务的数据库表,每个任务的状态都是“打开”或“关闭”。 In my case I have only records with open tasks.就我而言,我只有未完成任务的记录。

I need to build an array showing all status options (open, closed) and inside of each of them the tasks which are open or closed, if they exists.我需要构建一个数组,显示所有状态选项(打开、关闭)以及在每个状态选项内部打开或关闭的任务(如果存在)。 Output should be: Output 应该是:

Array (
  [open] => Attay(task1), Array(task2),..
  [closed] => Array()  
)

If I make the sql query:如果我进行 sql 查询:

select task_id,status from tasks group by status

I get only the tasks inside "open".我只得到“开放”内的任务。 Which is the best way to build the status Array and "append" inside of each of them the tasks with that status?哪种是构建状态数组并在每个数组中“附加”具有该状态的任务的最佳方法?

thanks in advance!提前致谢!

You can use GROUP_CONCAT to collect all the tasks for each status into a comma separated list and then explode that into an array in PHP.您可以使用GROUP_CONCAT将每个状态的所有任务收集到一个逗号分隔的列表中,然后将其分解为explode中的一个数组。 Assuming you are using mysqli and your connection is in $conn , you would do something like this:假设您使用的是mysqli并且您的连接位于$conn中,您将执行以下操作:

$sql = "SELECT status, GROUP_CONCAT(task_id) AS taskids FROM tasks GROUP BY status";
$result = $conn->query($sql);
$output = array();
while ($row = $result->fetch_assoc()) {
    $output[$row['status']] = explode(',', $row['taskids']);
}

In pure SQL, you can generate a derived table of statuses with union all , then bring your original table with a left join :在纯 SQL 中,您可以使用union all生成状态派生表,然后将原始表与left join

select
    s.status,
    json_arrayagg(t.task_id) json_task_ids
from (
    select 'open' status
    union all select 'closed' status
) s
left join tasks t on t.status = s.status
group by s.status

MySQL does not have an array datatype, so this brings the list of task ids as a json array. MySQL 没有array数据类型,因此这会将任务 ID 列表作为 json 数组。 Another option is to build a comma separated lists: for this, you can use group_concat() :另一种选择是构建一个逗号分隔的列表:为此,您可以使用group_concat()

select
    s.status,
    group_concat(t.task_id) csv_task_ids
from (
    select 'open' status
    union all select 'closed' status
) s
left join tasks t on t.status = s.status
group by s.status

You can try something like this你可以试试这样的

$sql = "SELECT task_id, status FROM tasks";

$status['open'], status['close'] = [];

while( $row = $result->fetch_assoc() ) {

    if ($row['status'] == 'open') {
        array_push($status['open'], $row['task_id']);
    }

    if ($row['status'] == 'close') {
        array_push($status['close'], $row['task_id']);
    }
}

print_r($status);

Thanks everybody for contributions.感谢大家的贡献。

I solved in this way.我是这样解决的。

  $statusArray = ['opened', 'wait_others', 'wait_us', 'closed'];
  foreach ($statusArray as $status) {
    $ticketsArray[$status]= array();
  }
  foreach ($row as $key => $value) {
    $ticketsArray[$value->status_title][$value->ticket_id]= $row[$key];
  }
  print"<pre>";print_r($ticketsArray);

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

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