dongshou1991 2013-05-17 18:20
浏览 42
已采纳

从两个不同的表中提取信息并将它们合并到新闻源[关闭]

I have two different tables with different structures. When user does action A, a row gets inserted into table A in respect to the action, the same way for table B. When user does action B, a row gets inserted into table B.

Now I have a news feed that I want to show both these actions to other friends. How do I query both tables with different structure using MySQL, and then use PHP to construct update posts in a time sorted manner?

Just like in Facebook, A user update on his relationship status is different than a post he makes and attach a photo to it but they both appear in the same news feed although their structures and the type of data they carry is different. I appreciate if someone can shed some light on this and discuss a possible solution.

  • 写回答

1条回答 默认 最新

  • doutuobao9736 2013-05-17 18:29
    关注

    The general approach would be something like this:

    SELECT
      'A' AS ActionType,
      ActionA_PK AS ActionPK,
      ActionA_Date AS ActionDate,
      <a description of action A> AS ActionDesc
    FROM ActionA
    UNION SELECT
      'B',
      ActionB_PK,
      ActionB_Date,
      <a description of action B>
    FROM ActionB
    ORDER BY ActionDate DESC
    

    That folds the different actions together and orders them by date, with the most recent first. You can display them on the front end, and because you have their type (A or B) and their primary key you can identify them when posting back for processing when their link or button or whatever is clicked.

    There's a SQLFiddle example here.

    The another SQLFiddle example here which explains how the tables don't need to have the same structure to make this work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!