Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从不同表中的数据创建时间线_Php_Mysql - Fatal编程技术网

Php 从不同表中的数据创建时间线

Php 从不同表中的数据创建时间线,php,mysql,Php,Mysql,我的脚本中有几个表,我需要从它们中创建一个时间表。主要的表格是“帖子”——“喜欢”——“评论” posts表中有行:PID-UID-title-time\u已添加。 表中有行:LID-PID-UID-time\u已添加。 注释表中有行:CID-PID-UID-time\u已添加。 users表中有行:UID-username。 我正在用户档案中创建一个名为“时间线”的页面。它应该显示按时间排序的用户操作。UID指的是用户ID 浏览该页面时的数据示例: 用户在xxx时对xxxx帖子发表了评论 用户

我的脚本中有几个表,我需要从它们中创建一个时间表。主要的表格是“帖子”——“喜欢”——“评论”

posts表中有行:PID-UID-title-time\u已添加。
表中有行:LID-PID-UID-time\u已添加。
注释表中有行:CID-PID-UID-time\u已添加。
users表中有行:UID-username。

我正在用户档案中创建一个名为“时间线”的页面。它应该显示按时间排序的用户操作。UID指的是用户ID

浏览该页面时的数据示例:

  • 用户在xxx时对xxxx帖子发表了评论
  • 用户喜欢在xxx时发布xxxx\u添加\u xxx
  • 用户在xxx时添加了帖子xxxx
  • 是否有一种方法或MySQL查询可以组合所有这些表并按添加的时间排列它们
  • 我考虑创建一个新的MySQL表:user\u timeline,其中包含行:

    TID - UID - PID - LID - CID
    
    在每个操作(Post、Like、Comment)之后,根据操作将新行插入该表中,并将操作ID添加到相应字段中,其他字段保留为空。然后,如果可能,将它们与相关表结合起来调用

  • 您可以使用和别名:

    select * from ((select 1 as type,time_added from posts where ...) union (select 2 as type,time_added from likes where ...) ...) order by time_added asc
    
    注意:对于列类型,列选择的顺序必须相同

    不要:

    (select 1 as type,time_added from posts where ...) union (select time_added,2 as type from likes where ...)
    
    或者,如果在子查询中选择的列数不相同:

    SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns
    
    结果集将是一个多维数组,如下所示:

    array(array('type'=>1,'time_added'=>'...'),array('type'=>2,'time_added'=>'...'));
    
    通过键入你知道这是一篇文章还是类似的文章
    希望这有帮助

    是的,这是可能的。最好是编写填充第二个表的查询,然后查询第二个表并按时间戳排序。