Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 SQL查询:对来自两个表的结果进行分组_Php_Mysql_Sql_Left Join - Fatal编程技术网

Php SQL查询:对来自两个表的结果进行分组

Php SQL查询:对来自两个表的结果进行分组,php,mysql,sql,left-join,Php,Mysql,Sql,Left Join,您好,提前谢谢。我有两张桌子: 用户 |- ID -|- NAME -| |- 1 -|- Will -| |- 2 -|- Isabel -| |- 3 -|- Robert -| |- 4 -|- John -| |- 5 -|- David -| |- 6 -|- Eric -| |- ID -|- USER -|- DESCRIPTION -| |- 1 -|- 1 -|- Clean floor -| |- 2 -

您好,提前谢谢。我有两张桌子:

用户

|- ID -|- NAME -| |- 1 -|- Will -| |- 2 -|- Isabel -| |- 3 -|- Robert -| |- 4 -|- John -| |- 5 -|- David -| |- 6 -|- Eric -| |- ID -|- USER -|- DESCRIPTION -| |- 1 -|- 1 -|- Clean floor -| |- 2 -|- 1 -|- Clean windows -| |- 3 -|- 2 -|- Repair an appliance -| |- 4 -|- 3 -|- Buy spare parts -| |- 5 -|- 1 -|- Remove stains -| |- 6 -|- 2 -|- Pick up the lounge -| |-ID-|-名称-| |-1-|-威尔-| |-2-|-伊莎贝尔-| |-3-|-罗伯特-| |-4-|-约翰-| |-5-|-大卫-| |-6-|-埃里克-| 任务

|- ID -|- NAME -| |- 1 -|- Will -| |- 2 -|- Isabel -| |- 3 -|- Robert -| |- 4 -|- John -| |- 5 -|- David -| |- 6 -|- Eric -| |- ID -|- USER -|- DESCRIPTION -| |- 1 -|- 1 -|- Clean floor -| |- 2 -|- 1 -|- Clean windows -| |- 3 -|- 2 -|- Repair an appliance -| |- 4 -|- 3 -|- Buy spare parts -| |- 5 -|- 1 -|- Remove stains -| |- 6 -|- 2 -|- Pick up the lounge -| |-ID-|-用户-|-说明-| |-1-|-1-|-清洁地板-| |-2-|-1-|-清洁窗户-| |-3-|-2-|-修理电器-| |-4-|-3-|-购买备件-| |-5-|-1-|-去除污渍-| |-6-|-2-|-选择休息室-| 我需要一些帮助来编写一个查询,以获取按“用户”分组的数组“任务”,如下所示:

Array ( [0] => Array ( [user] => 1 [tasks] => Array ( [0] => Array ( [id] => 1 [description] => Clean floor ) [1] => Array ( [id] => 2 [description] => Clean windows ) [2] => Array ( [id] => 5 [description] => Remove stains ) ) ) [1] => Array ( [user] => 2 [tasks] => Array ( [0] => Array ( [id] => 3 [description] => Repair an appliance ) [1] => Array ( [id] => 6 [description] => Pick up the lounge ) ) ) [2] => Array ( [user] => 3 [tasks] => Array ( [0] => Array ( [id] => 4 [description] => Buy spare parts ) ) ) ) 大堆 ( [0]=>阵列 ( [用户]=>1 [任务]=>阵列 ( [0]=>阵列 ( [id]=>1 [说明]=>清洁地板 ) [1] =>阵列 ( [id]=>2 [说明]=>清洁窗口 ) [2] =>阵列 ( [id]=>5 [说明]=>去除污渍 ) ) ) [1] =>阵列 ( [用户]=>2 [任务]=>阵列 ( [0]=>阵列 ( [id]=>3 [说明]=>修理设备 ) [1] =>阵列 ( [id]=>6 [说明]=>选择休息室 ) ) ) [2] =>阵列 ( [用户]=>3 [任务]=>阵列 ( [0]=>阵列 ( [id]=>4 [说明]=>购买备件 ) ) ) ) MySQL没有“数组”。也许使用
group\u concat()
可以满足您的需求:

select t.user,
       group_concat(t.id, ':', t.description order by t.id) as tasks
from tasks t
group by t.user;

我可能认为在表之间使用简单的左连接在这里是可行的。您是否尝试过查询,是否可以共享您的PHP代码?请先进行select join,然后在所需的结构中重新格式化数组为我对环境的错误表达和描述道歉。当然,我指的是用PHP获取数组。这个答案正是我所需要的。测试和工作。谢谢。