PHP连接表显示重复

时间:2014-07-23 18:22:47

标签: php mysql

我成功加入了两个表,但我无法弄清楚如何调整重复的变量。

$kelo = "SELECT e.id, e.rosary, e.group_name, e.description, u.group_id, u.name, u.decade, u.intention, u.datume FROM `rosary_group_name` AS e INNER JOIN `rosary_groups` AS u ON e.id = u.group_id ORDER BY e.id DESC, u.group_id DESC;";

    $melo = mysqli_query($con,$kelo);
    while($row = mysqli_fetch_array($melo)){
        echo '<div style="clear:left;">' .$row['rosary']. " - ". $row['name'].'</div>';
        echo "<br />";
    } 

显示是:

JOJO - hello1
JOJO - hello2
JOJO - hello3
PAJO - hi1
PAJO - hi2
KAJO - Cheers1
KAJO - Cheers2
KAJO - Cheers3

但我需要:

    JOJO - hello1
           hello2
           hello3
    PAJO - hi1
           hi2
    KAJO - Cheers1
           Cheers2
           Cheers3

我该如何实现?

2 个答案:

答案 0 :(得分:2)

这样的东西?

$melo = mysqli_query($con,$kelo);
$current = '';
while($row = mysqli_fetch_array($melo)){
    if($current != $row['rosary']) { 
        $current = $row['rosary']; 
        $display = $current . ' - '; 
    }
    else $display = '';

    echo '<div style="clear:left;"><span>' .$display . '</span><span>'.$row['name'].'</span></div>';
    echo "<br />";
} 

如果使用普通格式,可以使用CSS对齐它或sprintf。

答案 1 :(得分:0)

如果您的主要关注点是e.rosary,那么此查询可能对您有所帮助:

$kelo = "SELECT e.id, e.rosary, e.group_name, e.description, u.group_id, u.name, u.decade, u.intention, u.datume FROM `rosary_group_name` AS e INNER JOIN `rosary_groups` AS u ON e.id = u.group_id ORDER BY e.rosary ASC, e.id DESC, u.group_id DESC;";