SQL查询未按预期执行

时间:2014-10-26 02:49:53

标签: php sql

我正在尝试在页面上显示4列数据。每行是来自数据库(3)的信息,每个艺术家ID只显示一行(GROUP BY)

问题出在第4行,即“每行最后一次数据输入”。它只返回第一个条目。这对每个艺术家都是如此。

示例: - 艺术家ID 31在相册图像数据库中有7行,每行具有不同的时间戳。 因此,输出是艺术家图像(第1列)艺术家姓名(第2列)相册数量(第3列[计数]和最后输入日期(第4列)。即 - pic - ACDC - 7 - 2012年6月20日(这应该是2014年9月6日)

尝试了很多功能但无法修复。已经单独运行sql查询但是如果我删除GROUP BY和COUNT只能获得部分满意的结果。但这违背了目标。

非常感谢

功能 ------

<?php
function paged_artist($artist_id) {
$artists = array();
$per_page = 10;
$pages_query = mysql_query("SELECT Count(`artist_id`) FROM `artists`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$artists_query = mysql_query("
SELECT `artists`.`artist_id`, `artists`.`artist_name`, LEFT(`artists`.`artist_description`, 50) as `artist_description`, `album_images`.`timestamp`, `artist_images`.`image_artist_id`, `artist_images`.`artist_name`, `artist_images`.`ext`, COUNT(`album_images`.`image_album_id`) as `image_count`
FROM `artists`
LEFT JOIN `artist_images`
ON `artists`.`artist_id` = `artist_images`.`artist_id`
LEFT JOIN `album_images`
ON `artists`.`artist_id` = `album_images`.`artist_id`
WHERE `artists`.`member_id` = ".$_SESSION['member_id']."
GROUP BY `artists`.`artist_id`
ORDER BY `artists`.`artist_name`, `album`.`timestamp` DESC
LIMIT $start, $per_page
");

While ($artists_row = mysql_fetch_assoc($artists_query)) {
$artists[] = array(
'artist_id'              => $artists_row['artist_id'],
'artist_name'            => $artists_row['artist_name'],
'artist_description'     => $artists_row['artist_description']
'timestamp'              => $artists_row['timestamp'],
'image_artist_id'        => $artists_row['image_artist_id'],
‘ext'                    => $artists_row['ext'],
'count'                  => $artists_row['image_count']
 );
}
return $artists;
}

Output.php -------

<?php
$artists = paged_artist($artist_id);
if (empty($artists)) {
echo '<p>You Don\'t Have Any Albums</p>';
}else{
foreach ($artists as $artist) {
echo '
<tr>
<td class="alt2" align="center" valign="middle">
<a href="members_artists.php?artist_id=', $artist['artist_id'], '">
<img class="album_cover2" src="images/artists/thumbs/', $artist['artist_id'], '/',   $artist['image_artist_id'], '.', $artist['ext'], '" alt="" height="100" width="100" border="0" ></a>
</td>
<td class="alt1" valign="middle">
<a href="members_artist.php?artist_id=', $artist['artist_id'], '"><strong>', $artist['artist_name'], '</strong></a>
<div class="smallfont"></div>
</td>
<td class="alt2" align="center" valign="middle">', $artist['count'], '</td>
<td class="alt1" align="center" valign="middle">', date('dS F Y $artist['timestamp']), '
<span class="time"></span>
</td>
</tr>
<tr>
</td>
</tr>';
}
}
?>

0 个答案:

没有答案