dongshang5862 2015-10-21 12:03 采纳率: 0%
浏览 236
已采纳

使用SQL对表中的数据进行分类/分组

I have stored the physical locations of specific files in my database with download counters to provide downloads via shorter urls like /Download/a4s. Each file has a categoryId assigned via foreign keys which just describes to which course/lecture it belongs for an easier overview. The table fileCategories basically looks like this

categoryId | categoryName
---------------------------
         1 | Lecture 1
         2 | Lecture 2
         3 | Personal Stuff

Assume that I have a files table which looks like this with some other columns I did omit

fileId | categoryId | filePath     | ...
----------------------------------------
     1 |          1 | /Foo/Bar.pdf | ...
     2 |          1 | /Foo/Baz.pdf | ...
     3 |          2 | /Bar/Foo.pdf | ...
     4 |          2 | /Baz/Foo.pdf | ...
     5 |          3 | /Bar/Baz.pdf | ...

I have created a page which should display some data about those files and group them by their categories which produces a very simple html table which looks like this:

Id | Path         | ...
-----------------------
Lecture 1
-----------------------
 1 | /Foo/Bar.pdf | ...
 2 | /Foo/Baz.pdf | ...
-----------------------
Lecture 2
-----------------------
 3 | /Bar/Foo.pdf | ...
 4 | /Baz/Foo.pdf | ...
-----------------------
Personal Stuff
-----------------------
 5 | /Bar/Baz.pdf | ...

So far I am using multiple SQL queries to fetch and store all categories in PHP arrays and append file entries to those arrays when iterating over the files table. It is highly unlikely this is the best method even though the number of files is pretty small. I was wondering whether there is a query which will automatically sort those entries into temporary tables (just a spontaneous guess to use those) which I can output to drastically improve my current way to obtain the data.

  • 写回答

1条回答 默认 最新

  • dqbjvg2518 2015-10-21 12:11
    关注

    You can not do this with just mysql but a combination of JOIN and some PHP.

    SELECT * FROM files f LEFT JOIN fileCategories c USING (categoryId) ORDER BY c.categoryName ASC
    

    Be sure to order by the category first (name or ID) and optionally order by other params after that to allow the following code example to work as expected.

    in PHP then iterate over the result, remember the category id from each row and if it changes you can output the category delimiter. assumung the query result is stored in $dbRes

    Example Code:

    $lastCategoryId = null;
    while ($row = $dbRes->fetchRow()) {
        if ($lastCategoryId !== $row['categoryId']) {
            echo "--------------------" . PHP_EOL;
            echo $row['categoryName'] . PHP_EOL
            echo "--------------------" . PHP_EOL;
        }
    
        echo $row['filePath'] . PHP_EOL;
        $lastCategoryId = $row['categoryId'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?