Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/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 相同id的MYSQL组?_Php_Mysql - Fatal编程技术网

Php 相同id的MYSQL组?

Php 相同id的MYSQL组?,php,mysql,Php,Mysql,我有以下两个表格: 表1:用户 id s_id first_name last_name ---------------------------------- 1 2 test test 2 2 hello test 3 2 now hello 4 1 john smith 表2:章节 id sect

我有以下两个表格:

表1:用户

   id   s_id   first_name   last_name
   ----------------------------------
   1     2     test          test
   2     2     hello         test
   3     2     now           hello
   4     1     john          smith
表2:章节

   id    section_name
  -------------------
   1     first
   2     my section
   3     other section
基于以上两个表,我想写一个MYSQL查询:

从用户表中选择“全部”,并按s_id(相同的s_id)对它们进行分组,我如何才能做到这一点

我想把所有的s_id=2都分组为一个数组或对象,但由s_id控制?所以我会循环检查s_ID并打印出名字和姓氏

以下是一个输出示例:

$sections = array['my section'](array('first_name'=>'test' ,  'last_name'=>'test'),
                                array('first_name'=>'hello' ,  'last_name'=>'test'),
                                array('first_name'=>' now' ,  'last_name'=>'hello'))
非常感谢

SELECT * FROM `user` GROUP BY `s_id`
但是你在标题和内容上有不同的问题

因此,您在PHP中需要的是如下内容:

while($row = your sql fetch)
{
    $array[$row['s_id']][] = array('first_name' => $row['first_name'], 'last_name' => $row['last_name']);
}

print_r($array);

如果您需要
s_id=2
的所有用户,则:

SELECT * FROM `user` where `s_id` = 2 # this will give 3 users
否则,如果您使用
s_id
分组,则每个部分只会有一个用户,我认为您不希望这样

要显示全部,则必须运行查询:

SELECT user.*, section.section_name FROM `user`
INNER JOIN section ON section.id = user.s_id
并在PHP中使用技巧:

$query = "
    SELECT 
        user.*, 
        section.section_name 
    FROM `user`
    INNER JOIN section 
        ON section.id = user.s_id
";

$result = mysqli_query($conn, $query);

$final_data = array();
while($data = mysqli_fetch_assoc($result))
{
    $final_data[$data['section_name']][] = $data;
}

print($final_data); // to see the result

看看php中的
mysqli\u fetch\u array
函数

您需要使用此php函数循环查询结果并将其放入数组中,不需要在sql中对任何内容进行分组,只需:

按序列号从用户订单中选择*


在php循环中,您可以根据需要填充数组。

您可以展示一个您试图获得的输出的示例吗?当然,我会更新问题,我认为应该添加s_id=2。OP:“我想把所有的
s_id=2
都分组在一起”-但是,我可能错了。我的回答是:)那么这一个的输出是什么呢?我只返回结果,而不是3行?看看php中的mysqli_fetch_数组函数。您需要循环查询结果并使用此php函数将它们放入数组中,您不需要在sql中对任何内容进行分组,只需要使用其中的s_id=2,但我不想指定某个s_id=#,我想获取所有具有相同编号的结果,以便将所有s_dis=2和s_id=1放在一起,等等。。