dongtangjie0495 2014-09-16 18:15
浏览 29

定义媒体列表的属性并在一行中返回值

I want to build a media list which can hold different media types like movies, tv series, books, games and each of those types can have different attributes attached to them.

Right now I have the following tables:

categories

catid   | name
1       | Books
2       | Movies
3       | Tv Series

media

mid     | title         | catid
1       | Book 1        | 1
2       | Movie 1       | 2
3       | TV Series 1   | 3
4       | Book 2        | 1
5       | Movie 2       | 2

attributes

attrid  | catid | name
1       | 1     | number of pages
2       | 1     | description
3       | 1     | year
4       | 2     | length
5       | 2     | cast
6       | 2     | description
7       | 3     | cast
8       | 3     | description
9       | 3     | year
10      | 3     | number of episodes

attribute_info

attraid | mid   | attrid    | value
1       | 1     | 1         | 213
2       | 1     | 2         | Description...
3       | 1     | 3         | 1996
4       | 2     | 4         | 210 minutes 
5       | 2     | 5         | Actor1, Actor2
6       | 2     | 6         | Description...
7       | 3     | 7         | Actor3, Actor4
8       | 3     | 8         | Description...
9       | 3     | 9         | 2010
10      | 3     | 10        | 12

categories holds information about the different media types. media holds information about the individual media entry, attributes defines the different attibutes per category and attribute_info finally holds the info itself.

Now I'm looking for an efficient way to fetch the information via SQL Ideally I could group all information a media property has into one output row and get something like

Array([mid] => 1, [title] => Book 1, [catid] => 1, [name] => Books, [number of pages] => 213, [description] => Description..., [year] => 1996)

Ways to do this should be inner joins but I don't like to change my PHP code when I add an attribute to a category. What's an elegant in SQL way to achieve my goal?

  • 写回答

1条回答 默认 最新

  • dongweihuan8832 2014-09-16 20:03
    关注

    It might be easier to make two queries, one to get category and title, and then another to get all the attribute-attribute_info pairs for that mid. Then, append the second array to the first one.

    Of course, you're going to run into problems there if any of your attributes match one of your titles. :)

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端