【问题标题】:php join 2 tables by date problemphp按日期加入2个表的问题
【发布时间】:2011-06-01 22:15:25
【问题描述】:

所以我有 2 个表,它们没有任何共同的列,我想按它们的日期列存储它们

所以 table1 是这样的:

表1

  • 身份证
  • post_id
  • post_date

表2

  • 身份证
  • comment_id
  • comment_date

我要展示的是 table1,table2 中的所有内容并按日期排序

我试过类似的东西

SELECT * FROM table1 INNER JOIN table2 ORDER BY post_date DESC, comment_date DESC

问题是我不知道如何识别我在 while(rows = mysql_fetch_assoc()) 中使用的项目(帖子或评论),因为我有不同的列名。

解决方案是:

                SELECT * FROM (
                SELECT 1 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, `title` AS `title`, etc... , `date` AS `date` FROM `table1`
                UNION
                SELECT 2 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, NULL AS `title`, etc... , `date` AS `date` FROM `table2`
            ) AS tb
            ORDER BY `date` DESC

【问题讨论】:

  • 描述我不知道如何识别哪个项目(帖子或评论)
  • 所以可以说我的 table1 有一些记录,但 table2 仍然为空......我怎么知道要显示哪些数据?table1 或 table2 的?因为我在两个表上都有不同的列
  • 您的主题说您想按日期加入,但您的代码按 id 加入。这对我来说毫无意义。完全没有。你到底想完成什么?
  • 因为我使用 ORDER BY post_date DESC,comment_date DESC 不是加入 2 个表并按日期显示 DESC 吗?我试图按日期显示这 2 个表 DESC 中的所有记录
  • cmet 与帖子无关吗?

标签: mysql join


【解决方案1】:

尝试使用UNION,使用新的常量列指示正在输出哪个表,并使用AS 使列名相同。

周围的SELECT 可能允许您一起订购。

SELECT * FROM (
    (SELECT 1 AS `table`, `id`, `post_id` AS `table_id`, `post_date` AS `date` FROM `table1`)
    UNION
    (SELECT 2 AS `table`, `id`, `comment_id` AS `table_id`, `comment_date` AS `date` FROM `table2`)
)
ORDER BY `date` DESC

这需要测试,不确定是否允许。

【讨论】:

  • 上面的代码不能按我的意愿工作,我必须分配 2 个表中的所有列并具有相同的键名,例如 post_date=date 和 comment_date=date,我想要检查每次while循环运行(获取数据)时,我都从哪个表中获取数据。无论如何,谢谢。
  • @fxuser:这就是该代码应该发生的情况。您使用table 字段来确定来源。字段按要求统一。
  • 问题是在一个表上的列实际上与另一个表不匹配,并且我在显示数据时遇到了麻烦......我试试联合吧
  • @fxuser:当你说,不匹配?你是什​​么意思?如果您的意思是没有共享字段,那么 UNION 就是您所需要的。如果您的意思是它们是不同的数据类型,那么您必须进行某种转换或将数据分开。
  • @fxuser,不,您不需要,只需在缺少字段的表的选择中指定 , null as missing_field
【解决方案2】:

如果要将帖子链接到 cmets,则必须重新设计数据库。

鉴于这些表格

table1

    id
    post_id
    post_date

table2

    id
    comment_id
    comment_date

而cmets应该是属于posts的cmets,需要换个表,使其结构变成:

table post

    id         /*id of a post*/
    user_id    /*which user posted this*/
    post_date  /*when?*/
    post_text  /*the text inside the post*/

table comments

    id            /*id of a comment*/
    post_id       /*which post does this comment belong to*/
    user_id       /*who posted this*/
    comment_date  /*when*/
    comment_text  /*text of the comment*/

现在您可以通过以下方式加入:

$post_id = mysql_real_escape_string($_GET['post_id']);

/*select all comments belonging to a post*/
$query = "SELECT c.user_id, c.comment_date, c.comment_text FROM posts p
          INNER JOIN comments c ON (c.post_id = p.id)
          WHERE p.id = '$post_id'";
....

在您当前的设计中,没有办法可靠地加入它们,因为两者之间没有关系。

【讨论】:

  • 我没有说清楚,这2个表是一个简单的例子,我想按添加到数据库中的日期降序显示它们,table1可以是链接,table2可以是tas等等,我只想知道他们如何按添加日期降序
  • @fxusers,@Orbling 已经回答了这个问题,你应该接受他的回答。还是你的例子没有任何意义,功课?
  • 这不是功课,我只是为网络应用创建一些脚本
【解决方案3】:
                SELECT * FROM (
            SELECT 1 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, `title` AS `title`, etc... , `date` AS `date` FROM `table1`
            UNION
            SELECT 2 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, NULL AS `title`, etc... , `date` AS `date` FROM `table2`
        ) AS tb
        ORDER BY `date` DESC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2020-12-11
    • 2016-08-24
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多