php - 如何从php/mysql中的同一张表中只获取一次重复值和多次重复值

标签 php mysql database

我有 2 个表。一个是帐户,另一个是预订。

account table

booking table

我想从预订表中获取数据,因为重复数据应该只显示一次,但所有开始字段的数据都来自所选的 UId。采用以下格式。

日期 : 2014-12-20 开始: 7:00,7:30,8:00,8:30,9:00,9:30 玩家: 0 评论:测试预订

$comp_query1=mysql_query("select * from bookings where UId = '$contact_id'");
    while($booking=mysql_fetch_array($comp_query1))
    {
    ?>
    <tr>
<td><strong>comments : </strong></td>
<td><?php echo $booking['comments']; ?></td>
</tr>
     <tr>
<td><strong>Date : </strong></td>
<td><?php echo $booking['date']; ?></td>
</tr>

<tr>
<td><strong>Players : </strong></td>
<td><?php echo $booking['Players']; ?></td>
</tr>
<tr>
<td><strong>Start : </strong></td>
<td>    <?php echo $booking['start']; ?>


 </td>
</tr>

这是我的查询。帮我。 提前谢谢你。

最佳答案

只需使用您的 key 创建一个数组,如果您已经拥有它,只需将 data 附加到您已存储的值即可。

类似的东西(翻译成 PHP):

while () {
  if (!array_containskey($bookings, $booking[id])){
    $bookings[$booking[id]] = $booking;
  } else {
    bookings[$booking[id]][date] .= ",".$booking[date] ;// append
  }
}

然后您可以使用正确的值在新数组上循环。

关于php - 如何从php/mysql中的同一张表中只获取一次重复值和多次重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27598505/

相关文章:

php - tinymce b.editor 为空,this.params 在 FF 上未定义

javascript - 带有日期时间选择器的可编辑数据网格

php - 从 MySQL TEXT 列中提取时在 HTML 中显示空格

android - SimpleAdapter 每秒崩溃一次

php - 数据库日期 = 今天的日期

php动态生成图像映射坐标

php - 趋势 SQL 查询

database - 如何同步 postgresql 模式的表 - Sequelize ORM

database - Hibernate Criteria 一对多关系

PHP MySQL 从临时列中获取数据