【发布时间】:2011-08-19 02:43:22
【问题描述】:
假设一个元素,例如youtube 视频可以是各种类别,例如音乐、运动、娱乐。
我想查看数据库中的所有元素并输出最常出现的类别的有序列表。
编辑: 具体来说,有三个相关的数据库表。 1) item (name, id) 2) category (name, id) 3) item_category (item_id, category_id)
1) 还有什么比这更有效的吗?:
Pseudocode
foreach (category)
select all database elements which match that category
associate category with the number of results from the select query
return the associative array
sort the associative array on the values of each category (key)
2) 什么 PHP 函数可以让我轻松地对关联数组的值进行排序?或者什么 SQL 可以让我更有效地做到这一点?
【问题讨论】:
-
您想在数据库中还是在 PHP 中执行此操作?该数据库通常更适合过滤和排序数据...
-
这让我做得更高效