我是 MySQL 的新手,只需阅读我书中解释它的部分,现在我将它应用到我正在构建的网站上。
我的数据库中有以下两个表:
Table Name: Transactions
+------------------+------------+-----------+--------+
| description | date | category | amount |
+------------------+------------+-----------+--------+
| Internet | 2013-01-04 | Bills | $50.00 |
+------------------+------------+-----------+--------+
| Giant Eagle Trip | 2013-10-04 | Groceries | $30.00 |
+------------------+------------+-----------+--------+
| Car Insurance | 2013-01-04 | Bills | $90.00 |
+------------------+------------+-----------+--------+
| Starbucks | 2013-09-04 | Coffee | $5.00 |
+------------------+------------+-----------+--------+
| Giant Eagle Trip | 2013-11-04 | Groceries | $15.00 |
+------------------+------------+-----------+--------+
Table Name: Categories
+------+-----------+
| rank | Name |
+------+-----------+
| 1 | Bills |
+------+-----------+
| 2 | Groceries |
+------+-----------+
| 3 | Coffee |
+------+-----------+
在我的网站上,我想获取该交易表并按类别列对其进行排序。诀窍是我不希望它按字母顺序排列,我希望它按与类别表相同的顺序排列。我在下面显示的最终结果:
+------------------+------------+-----------+--------+
| description | date | category | amount |
+------------------+------------+-----------+--------+
| Internet | 2013-01-04 | Bills | $50.00 |
+------------------+------------+-----------+--------+
| Car Insurance | 2013-01-04 | Bills | $90.00 |
+------------------+------------+-----------+--------+
| Giant Eagle Trip | 2013-10-04 | Groceries | $30.00 |
+------------------+------------+-----------+--------+
| Giant Eagle Trip | 2013-11-04 | Groceries | $15.00 |
+------------------+------------+-----------+--------+
| Starbucks | 2013-09-04 | Coffee | $5.00 |
+------------------+------------+-----------+--------+
我想到了两种方法来做到这一点,它们如下:
//Option A: using arrays
//Get categories in correct order
$query = "select name
from Categories
order by rank";
$result = $db->query($query);
$numOfCategories = $result->num_rows;
for($i=0; $i<$numOfCategories; $i++) {
$row = $result->fetch_assoc();
$category[$i] = $row['name'];
}
//Get transactions and put in correct order
$query = "select *
from Transactions";
$result = $db->query($query);
$numOfTransactions = $result->num_rows;
for($i=0; $i<$numOfTransactions; $i++) {
$row = $result->fetch_assoc();
for($j=0; $j<$numOfCategories; $j++) {
if ($category[$j] == $row['category']) {
$tableRows[$j][] = $row;
}
}
}
//Display Table
echo "<table>\n";
for($i=0; $i<count($tableRows); $i++) {
for($j=0; $j<count($tableRows[$i]); $j++) {
echo " <tr>\n";
echo " <td>".$tableRows[$i][$j]['description']."</td>\n";
echo " <td>".$tableRows[$i][$j]['date']."</td>\n";
echo " <td>".$tableRows[$i][$j]['category']."</td>\n";
echo " <td>".$tableRows[$i][$j]['amount']."</td>\n";
echo " </tr>\n";
}
}
echo "</table><br><br>\n";
//Option B: using where in select query
//Get categories in correct order
$query = "select name
from Categories
order by rank";
$result = $db->query($query);
$numOfCategories = $result->num_rows;
for($i=0; $i<$numOfCategories; $i++) {
$row = $result->fetch_assoc();
$category[$i] = $row['name'];
}
//Get transactions and display table
echo "<table>\n";
for($i=0; $i<$numOfCategories; $i++) {
$query = "select *
from Transactions
where category = '".$category[$i]."'";
$result = $db->query($query);
for($j=0; $j<$result->num_rows; $j++) {
$row = $result->fetch_assoc();
echo " <tr>\n";
echo " <td>".$row['description']."</td>\n";
echo " <td>".$row['date']."</td>\n";
echo " <td>".$row['category']."</td>\n";
echo " <td>".$row['amount']."</td>\n";
echo " </tr>\n";
}
}
echo "</table><br><br>\n";
哪种方式更好以及为什么,或者是否有另一种方式比这些方式更好(可能使用我的书没有详细解释的 MySQL 中的索引功能和/或使用我的书也没有解释的准备好的语句非常详细)。