如何在PHP中对相关的MySQL记录进行分组?

时间:2015-01-25 16:08:12

标签: php mysql relational

我在PHP中遇到SQL查询问题。我已经尝试了一切,但它不想打球。输出如下:

Pat48
48
47
47
46
46
Jana47
Luc46 

我需要它成群结队,例如。

Pat48
48
48
Jana47
47
47
Luc46 
46
46

代码如下:

<?php
//Let's connect
$conn = mysql_connect("localhost", "username", "password");
mysql_select_db("the database");
$get_albums = "SELECT album_id, album_title FROM albums ORDER BY album_id    DESC";
$get_pictures = "SELECT picture_id, picture_title, picture_link, album FROM pictures WHERE album=album ORDER BY picture_id DESC LIMIT 1, 18446744073709551615";
$albums = mysql_query($album);
$first_row = mysql_fetch_assoc($first);
$picture_result = mysql_query($get_pictures);

$album_result = mysql_query($get_albums);
while($first_row = mysql_fetch_array($album_result))
{
    echo "<p>{$first_row['album_title']}{$first_row['album_id']}
    </p>";
    {
        while(list($picture_id, $picture_title, $picture_link, $album) =     mysql_fetch_row($picture_result))
        {
        echo "<p>{$album}</p>";
        }
    }
}
?>

感谢你们,这似乎有效

$get_albums = "SELECT album_id, album_title FROM albums ORDER BY album_id DESC";
$album_result = mysql_query($get_albums);

while($first_row = mysql_fetch_assoc($album_result))
{
    echo "<p>{$first_row['album_title']}</p>";

    $get_pictures = "SELECT picture_id, picture_title, picture_link, album FROM pictures WHERE album={$first_row['album_id']} ORDER BY album DESC LIMIT 1, 18446744073709551615";
    $picture_result = mysql_query($get_pictures);

    while($picture_row = mysql_fetch_assoc ($picture_result))
    {
        echo "<p>{$picture_row['picture_title']}</p>";
    }
}

现在我遇到以下问题。

我正在尝试将其与彩盒相结合,但现在当我点击一个链接时,它会在每个幻灯片中输出所有相同的4个第一页。代码如下。

$get_albums = "SELECT album_id, album_title FROM albums ORDER BY album_id DESC";
$album_result = mysql_query($get_albums);

while($first_row = mysql_fetch_assoc($album_result))
{
    $first = mysql_query("SELECT picture_id, picture_title, picture_link, album FROM pictures WHERE album={$first_row['album_id']} ORDER BY album DESC LIMIT 1");
    $first_picture = mysql_fetch_assoc($first);

    echo "<p><a class='group1' title=\"{$first_picture['picture_title']}\"   href={$first_picture['picture_link']}>{$first_row['album_title']}</a></p>";

    $get_pictures = "SELECT picture_id, picture_title, picture_link, album FROM pictures WHERE album={$first_row['album_id']} ORDER BY album DESC LIMIT 1, 18446744073709551615";
    $picture_result = mysql_query($get_pictures);

    while($picture_row = mysql_fetch_assoc ($picture_result))
    {
        echo "<p><a class='group1' title=\"{$picture_result['picture_title']}\"     href={$picture_result['picture_link']}>{$first_row['album_title']}</a></p>";
    }
}
?>

1 个答案:

答案 0 :(得分:0)

图片查询应位于每张专辑的循环内, mysql不会自动加入这两个查询,你需要使用第一个查询返回的php值将相册id传递给第二个查询,但是在你循环相册的循环中。

这样的事情:

<?php
//Let's connect
$conn = mysql_connect("localhost", "username", "password");
mysql_select_db("the database");
$get_albums = "SELECT album_id, album_title FROM albums ORDER BY album_id    DESC";
$album_result = mysql_query($get_albums);

while($first_row = mysql_fetch_assoc($album_result)){
    echo "<p>{$first_row['album_title']}{$first_row['album_id']}</p>";

    $get_pictures = "SELECT picture_id, picture_title, picture_link, album FROM pictures WHERE album_id={$first_row['album_id']} ORDER BY picture_id DESC";
    $picture_result = mysql_query($get_pictures);

    while(list($picture_id, $picture_title, $picture_link, $album) =     mysql_fetch_row($picture_result)){
        echo "<p>{$picture_id}</p>";
    }

}
?>