SELECT查询,LIMIT 1表示一列,但不是其余列

时间:2011-11-18 05:47:31

标签: php mysql

我可以为一个特定列限制1而不是其余列吗?我有两张桌子。一个有信息,一个有图像。对于尽可能多的图像,运行许多信息查询。因此,它将是5个图像和5个相同的标题。我怎样才能限制1个标题,限制3个图像?

$query = mysql_query("
                     SELECT `$category`.title AS title,
                     page_images.image_loc AS images
                     FROM `$category`, page_images
                     WHERE `$category`.title = page_images.title
                    ");

            while($fetch = mysql_fetch_assoc($query)) {

                $title  = $fetch['title'];              
                $images = $fetch['images'];

                echo '<b>'.$title.'</b><br />';
                echo '<a href="http://localhost/'.$images.'">
                        <img src="http://localhost/'.$images.'" height="80" width="80" />
                      </a>';

                }

2 个答案:

答案 0 :(得分:5)

您可以按标题分组并使用GROUP_CONCAT收集图像位置。然后您可以将收集的位置分解为数组。并打印所有图像。

这就是我的意思

$query = mysql_query( "
SELECT
     title AS title
    ,GROUP_CONCAT( i.image_loc ) AS images
FROM
    `$category` c
    JOIN page_images i USING( title )
GROUP BY
    title
" );

while( $fetch = mysql_fetch_assoc( $query ) ) {
    $title  = $fetch[ 'title' ];              
    $images = explode( ',', $fetch[ 'images' ] );

    echo '<b>' . $title . '</b><br />';
    foreach( $images as $image ) {
        echo '<a href="http://localhost/'.$image.'">
            <img src="http://localhost/'.$image.'" height="80" width="80" />
        </a>';
    }
}

答案 1 :(得分:1)

一群人在这里工作吗?

SELECT `$category`.title AS title,
    page_images.image_loc AS images
FROM    `$category`, page_images
WHERE   `$category`.title = page_images.title
GROUP BY title, images
LIMIT 3