如何在php/mysql中从同一个表中只获取一次重复的值,而获取另一次重复的值

时间:2022-12-23 03:36:57

i have 2 tables.one is accounts and other is bookings.

我有两个表。一个是账户,另一个是预订。

如何在php/mysql中从同一个表中只获取一次重复的值,而获取另一次重复的值

如何在php/mysql中从同一个表中只获取一次重复的值,而获取另一次重复的值

i want to get data from booking table as duplicate data should display only once but all the data for start field from the selected UId. in the following format.

我想从预订表中获取数据,因为重复的数据应该只显示一次,但是从所选的UId中显示start字段的所有数据。在以下格式。

date : 2014-12-20 start : 7:00,7:30,8:00,8:30,9:00,9:30 players : 0 comments : test booking

日期: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>

this is my query. Help me. Thank u in advance.

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

3 个解决方案

#1


2  

Just create an array with your key, if you already have it just append the data to the value you already stored.

只要用你的键创建一个数组,如果你已经有了它,只需将数据附加到你已经存储的值。

Something like (To translate into PHP) :

类似(翻译成PHP):

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

Then you can loop on your new array with correct values.

然后可以用正确的值对新数组进行循环。

#2


0  

select `date` , 
GROUP_CONCAT(
`start` order by `start` asc
) as `start`,

GROUP_CONCAT(
comments order by `start` asc
) as comments,
SUM(`Players`) as `Players`
from bookings 
where UId = '$contact_id'
group by `date`;

This should result in

这将导致

date : 2014-12-20 start : 7:00,7:30,8:00,8:30,9:00,9:30 players : 0 comments : test booking, test booking ,....

日期:2014-12-20开始:7点,7点半,8点,8,9,9:30球员:0评论:测试预订,测试预订,....

Then you could simply loop through the different row for each date and display the results

然后,您可以简单地循环遍历每个日期的不同行并显示结果。

#3


0  

i got the simplest way to do that by using implode function

我用内爆函数得到了最简单的方法

 <?php 
    $comp_query1=mysql_query("select * from bookings where UId = '$contact_id'");
    $book = array();
    while($booking=mysql_fetch_array($comp_query1))
    {
        $comments = $booking['comments'];
        $date = $booking['date'];
        $players = $booking['Players'];
        $book[] = $booking['start'];
        $comma_separated = implode(' | ', $book);
    }
    ?>
    <tr>
<td><strong>comments : </strong></td>
<td><?php echo $comments; ?></td>
</tr>
     <tr>
<td><strong>Date : </strong></td>
<td><?php echo $date; ?></td>
</tr>

<tr>
<td><strong>Players : </strong></td>
<td><?php echo $players; ?></td>
</tr>
<tr>
<td><strong>Start : </strong></td>
<td>    <?php echo $comma_separated; ?>


 </td>
</tr>

#1


2  

Just create an array with your key, if you already have it just append the data to the value you already stored.

只要用你的键创建一个数组,如果你已经有了它,只需将数据附加到你已经存储的值。

Something like (To translate into PHP) :

类似(翻译成PHP):

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

Then you can loop on your new array with correct values.

然后可以用正确的值对新数组进行循环。

#2


0  

select `date` , 
GROUP_CONCAT(
`start` order by `start` asc
) as `start`,

GROUP_CONCAT(
comments order by `start` asc
) as comments,
SUM(`Players`) as `Players`
from bookings 
where UId = '$contact_id'
group by `date`;

This should result in

这将导致

date : 2014-12-20 start : 7:00,7:30,8:00,8:30,9:00,9:30 players : 0 comments : test booking, test booking ,....

日期:2014-12-20开始:7点,7点半,8点,8,9,9:30球员:0评论:测试预订,测试预订,....

Then you could simply loop through the different row for each date and display the results

然后,您可以简单地循环遍历每个日期的不同行并显示结果。

#3


0  

i got the simplest way to do that by using implode function

我用内爆函数得到了最简单的方法

 <?php 
    $comp_query1=mysql_query("select * from bookings where UId = '$contact_id'");
    $book = array();
    while($booking=mysql_fetch_array($comp_query1))
    {
        $comments = $booking['comments'];
        $date = $booking['date'];
        $players = $booking['Players'];
        $book[] = $booking['start'];
        $comma_separated = implode(' | ', $book);
    }
    ?>
    <tr>
<td><strong>comments : </strong></td>
<td><?php echo $comments; ?></td>
</tr>
     <tr>
<td><strong>Date : </strong></td>
<td><?php echo $date; ?></td>
</tr>

<tr>
<td><strong>Players : </strong></td>
<td><?php echo $players; ?></td>
</tr>
<tr>
<td><strong>Start : </strong></td>
<td>    <?php echo $comma_separated; ?>


 </td>
</tr>