简体   繁体   English

使用HTML表格订购SQL查询

[英]Using html table to order a sql query

I have to organise a table of results by artist and album alphabetically, then track by track number. 我必须按艺术家和专辑的字母顺序组织一张结果表,然后按曲目号进行跟踪。 I have no idea how to go about this, any suggestions would be helpful. 我不知道该如何处理,任何建议都会有所帮助。 This is what i have so far: 这是我到目前为止所拥有的:

$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>

I'm not sure how to do so many orders in the query before it outputs to the html table 我不确定在输出到html表之前如何在查询中执行这么多订单

EDIT: I couldn't get the order by to work, but turns out i was having syntax issues with my query the way it was appended this worked : 编辑:我无法通过命令来工作,但事实证明我在查询中存在语法问题,因为它的附加方式是这样的:

" order by... “订购...

This didn't "order by 这不是“命令

just add order by fields(ASC or DESC) you want, separated by comma statment to end of your query: 只需order by fields(ASC or DESC) you want, separated by comma添加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

or 要么

order by 1,2,3,4 -- or 2,1,3,4 or 4,1,3,2 or etc

you can also add asc (for ascending order which is default) and dsc (for descending order) in front of columns in order by clause. 您还可以在order by子句的列前面添加asc (用于默认的升序)和dsc (用于降序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM