以数据库字段分组显示数据的sql语句(详细介绍)

时间:2021-05-23

复制代码 代码如下:
<?php
$dsn = "mysql:host=localhost;dbname=group";
$db = new PDO($dsn, 'root', '');
$db->exec('set names utf8');

$sql = "select conf_id from `group` group by conf_id";
//$pre = $db->prepare($sql);
//$result_arr = array();
$pre = $db->query($sql);
$result_arr = $pre->fetchAll(PDO::FETCH_ASSOC);

//print_r ($result_arr);
foreach ($result_arr as $v)
{
echo $v[conf_id]."<br>";
$vv = $v[conf_id];
$re = array();
$sql1 = "select * from `group` where conf_id = '$v[conf_id]' order by time";
$sql2 = "select * from `group` where conf_id = '$v[conf_id]' order by time DESC";
$rs1 = $db->query($sql1);
$rs2 = $db->query($sql2);
$re1[$vv] = $rs1->fetchAll(PDO::FETCH_ASSOC);
$re2[$vv] = $rs2->fetchAll(PDO::FETCH_ASSOC);
print_r($re1[$vv][0]);
print_r($re2[$vv][0]);
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章