我必须按歌手和专辑的字母顺序组织一个结果表,然后按曲目编号组织曲目。我不知道该怎么做,任何建议都会有帮助。这就是我目前所拥有的:
$dbQuery=$db->prepare("select tracks.title, albums.title, artists.name,
basket.id ".
"from basket,tracks,albums,artists ".
"where basket.userID=:userID ".
"and basket.paid='Y' ".
"and basket.trackID=tracks.id ".
"and albums.id=tracks.albumID ".
"and artists.id=tracks.artistID");
$dbParams = array('userID'=>$_SESSION["currentUserID"]);
$dbQuery->execute($dbParams);
$numTracks=$dbQuery->rowCount();
if ($numTracks==0)
echo "<h3>You have not made any purchases</h3>";
else {
?>
<h2>Your Purchase History</h2>
<table style="margin-left:15px">
<tr><th>Artist</th><th>Album</th><th>Track</th></tr>
<?php
while ($dbRow=$dbQuery->fetch(PDO::FETCH_NUM)) {
echo "<tr><td>$dbRow[2]</td><td>$dbRow[1]</td><td>$dbRow[0]</td><td>$dbRow[3]</td></tr>";
}
}
?>
</table>
</body>
在输出到html表之前,我不知道如何在查询中完成这么多订单
编辑:我无法获得订单,但事实证明,我的查询在附加方式上存在语法问题
"订单…
这并没有"按排序
只需将order by fields(ASC or DESC) you want, separated by comma
语句添加到查询的末尾:
select tracks.title, albums.title, artists.name, basket.id
from table_name
....
order by tracks.title, albums.title, artists.name, basket.id
或
order by 1,2,3,4 -- or 2,1,3,4 or 4,1,3,2 or etc
您还可以按子句的顺序在列前面添加asc
(默认为升序)和dsc
(默认为降序)。