在mysql中获取类似的行数

时间:2015-12-31 07:36:10

标签: php mysql

我正在开展一个项目,教师可以在这个项目中保存课堂时间。根据日子,他可以看到他的时间安排。我用了代码

$qry = mysqli_query($con, 'select * from users left join time_slot on users.id=time_slot.u_id where users.id=' . $id);
echo '<table class="table_class" border="2">
                <tr>
                <th class="details">id</th>
                <th class="details">Date</th>
                <th class="details">start time</th>
                <th class="details">End Time</th>
                </tr>';
while ($row = mysqli_fetch_array($qry)) {
    echo '<tr>';
    echo '<td class="details">' . $row['id'] . '</td>';
    echo '<td class="details">' . $row['name'] . '</td>';
    echo '<td class="details">' . $row['day'] . '</td>';
    echo '<td class="details">' . $row['time_from'] . '</td>';
    echo '<td class="details">' . $row['time_to'] . '</td>';
    echo '</tr>';
}
echo '</table>';

但它显示了导师在同一天有多个班级的多次。 我想表明他是否在同一天(星期一)有2个或更多课程,然后所有时间段显示在一行中。对于一周中的所有日子都是如此。我该怎么办?

2 个答案:

答案 0 :(得分:3)

您可以使用GROUP_CONCAT功能。假设你的ddl是那样的

create table users(id bigint, name varchar(50));
create table time_slot(id bigint, u_id bigint, day datetime, time_from time, time_to time);

sql如下:

select u.id,u.name, ts.day, 
group_concat(ts.time_from, ' - ', ts.time_to ORDER BY ts.time_from, ts.time_to)
from users u left outer join time_slot ts on u.id = ts.u_id
group by u.id, u.name, ts.day
order by u.name, ts.day

请参阅fiddle

答案 1 :(得分:0)

我已经做了一些临时值。 如果你想以同样的方式表达,那么它对你有用。

复制代码并在此处查看http://phpfiddle.org/

$obj1['id']='1';
$obj1['name']='a1';
$obj1['day']='asdadh';
$obj1['time_from']='1';
$obj1['time_to']='1';

$obj2['id']='2';
$obj2['name']='a2';
$obj2['day']='asdad';
$obj2['time_from']='1';
$obj2['time_to']='1';

$obj3['id']='3';
$obj3['name']='a2';
$obj3['day']='asdad';
$obj3['time_from']='1';
$obj3['time_to']='1';


$arr = Array();
$arr[]=$obj1;
$arr[]=$obj2;
$arr[]=$obj3;


echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';

foreach($arr as $row)
{
    echo '<tr>';
    echo '<td class="details">' . $row['id'] . '</td>';
    echo '<td class="details">' . $row['name'] . '</td>';
    echo '<td class="details">' . $row['day'] . '</td>';
    echo '<td class="details">' . $row['time_from'] . '</td>';
    echo '<td class="details">' . $row['time_to'] . '</td>';
    echo '</tr>';
}
echo '</table>'; 

echo "<br><br><br><br><br><br><br>";

$dates=Array();
$count=0;
foreach($arr as $id=>$row){
    $val = $row['day'];
    $key = array_search($val,$dates);
    if(is_numeric($key)){
        $arr[$key]['day']=$dates[$key].','.$val;
        unset($arr[$id]);
    }else{
        $dates[$count]=$val;
    }
    $count++;
}

// new table 

echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
    echo '<tr>';
    echo '<td class="details">' . $row['id'] . '</td>';
    echo '<td class="details">' . $row['name'] . '</td>';
    echo '<td class="details">' . $row['day'] . '</td>';
    echo '<td class="details">' . $row['time_from'] . '</td>';
    echo '<td class="details">' . $row['time_to'] . '</td>';
    echo '</tr>';
}
echo '</table>';