1

我是 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 中的索引功能和/或使用我的书也没有解释的准备好的语句非常详细)。

4

2 回答 2

2
SELECT Categories.*, Transactions.*
FROM Categories
LEFT JOIN Transactions ON Categories.name = Transactions.Category
ORDER BY Categories.rank

我建议不要在您的交易表中嵌入类别名称。虽然像这样一个简单的例子是可以的,但它最终会占用大量空间来存储所有这些类别名称的冗余副本。您应该只存储类别 ID。如果该表变得“大”,则 4byte int 与 varchars 的许多字节将大大节省。

于 2013-04-11T03:53:40.510 回答
0

利用JOIN

SELECT * FROM Transactions LEFT JOIN
   Categories ON Categories.Name = Transactions.category

More for LEFT JOIN

于 2013-04-11T03:53:29.187 回答