- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将所有视频以及每个视频的详细信息存储在数据库中。我有一些 table :
每个视频都有针对 1080 和 720 编码的文件,并且每个文件都再次使用三种不同的比特率进行编码,并且每个比特率再次使用 WebM 和 MP4 进行编码。因此,每个视频条目的 video_file 中有六个条目。所以我的查询返回每个视频 12 行。
我看到的问题是,对于每一行,都包含所有信息,而不仅仅是“neccecary”信息。示例:
| id | name | playlist | file | quality | size | type |
---------------------------------------------------------------------
1 video1 funny videos file1 3 720 MP4
1 video1 funny videos file2 2 720 MP4
1 video1 funny videos file3 1 720 MP4
1 video1 funny videos file4 3 1080 MP4
1 video1 funny videos file5 2 1080 MP4
1 video1 funny videos file6 1 1080 MP4
1 video1 funny videos file7 3 1080 WebM
1 video1 funny videos file8 2 1080 WebM
1 video1 funny videos file9 1 1080 WebM
1 video1 funny videos file10 3 720 WebM
1 video1 funny videos file11 2 720 WebM
1 video1 funny videos file12 1 720 WebM
这是一个示例输出。我认为更有效的方法是将结果分组,这样您就不会从行到行获得冗余信息,毕竟,您将用 PHP 或您正在使用的语言对这些信息进行分组以便能够使用它完全没有。
我认为更有效的示例:
| id | name | playlist | file | quality | size | type |
---------------------------------------------------------------------
1 video1 funny videos file1 3 1080 WebM
file2 MP4
file3 720 WebM
file4 MP4
file5 2 1080 WebM
file6 MP4
file7 720 WebM
file8 MP4
file9 1 1080 WebM
file10 MP4
file11 720 WebM
file12 MP4
这样你就可以轻松设置一些与列名对应的变量:
id, name, playlist, file, quality, size, type
while (record = fetchRecord)
id = record.id or id
name = record.name or name
playlist = record.playlist or playlist
file = record.file or file
quality = record.quality or quality
size = record.size or size
type = record.type or type
// Now you have filled the variables with the exact information you had in the first recordset example, but without wasting so much unnecessary bandwidth and processing power.
现在,我在另一个表中添加在视频播放期间弹出的注释,因此如果视频上有 x 个注释,则每个视频将返回 12x 行。
这太疯狂了!我错过了什么吗?我在这里“解释”的事情实际上可以做到吗?或者是像现在一样让一个查询返回每个视频 12 行,然后运行每个视频来获取笔记,还是什么更好?
我的问题到此结束,非常感谢您从头到尾阅读本文!
最佳答案
执行此类操作的另一种方法是在不同的列上使用 GROUP_CONCAT
函数,并对重复的列进行分组。这将在输出中创建一个分隔字符串列,您需要在 PHP 中解析该字符串;但是,它会将行数减少到仅非分组列的唯一组合的数量。例如:
select name, playlist, GROUP_CONCAT(file, '|', quality, '|', size, '|', type SEPARATOR '/') AS delimited_pairs
from video
group by name, playlist;
在上面的示例中,生成的 delimited_pairs
列将使用竖线字符分隔每个名称/播放列表(分组列)的每个唯一列值,并且每个唯一组合都将由前向分隔斜线。
关于php - 你能用数据库做到这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16855775/
我是一名优秀的程序员,十分优秀!