【问题标题】:How to write a query to get result in mysql如何编写查询以在 mysql 中获取结果
【发布时间】:2015-07-17 18:41:45
【问题描述】:

我有两个mysql表posts和follow,其中follow包含followid、from_user_id、to_user_id、日期、状态,post包含标题、描述、createdby、createdtime 我想编写一个查询来获取最新添加的帖子,该帖子由关注特定用户的用户发布。这意味着只是我们的 Facebook 页面,以获取我们的朋友发布到我们页面的帖子。任何人都可以立即提供帮助。

【问题讨论】:

  • 以你目前的数据库结构,不可能
  • 应该如何从段落格式中消化您的表格结构?我们不必那么努力地可视化您的问题。自己阅读这篇文章,就好像你要回答它一样

标签: php mysql codeigniter


【解决方案1】:

好的,我将您的架构稍微更改为如下所示:

跟随表格

+------------------+----------+------+-----+---------+-------+
| Field            | Type     | Null | Key | Default | Extra |
+------------------+----------+------+-----+---------+-------+
| user_id          | int(11)  | NO   | MUL | NULL    |       |
| followin_user_id | int(11)  | YES  |     | NULL    |       |
| date             | datetime | YES  |     | NULL    |       |
| status           | bit(1)   | YES  | MUL | NULL    |       |
+------------------+----------+------+-----+---------+-------+

帖子表

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| post_id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255) | YES  |     | NULL    |                |
| description | varchar(255) | YES  |     | NULL    |                |
| createdby   | int(11)      | YES  | MUL | NULL    |                |
| createdtime | timestamp    | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

让我们在两个表格帖子中填充一些虚拟数据并像这样跟随

+---------+-------------+-----------------------------+-----------+---------------------+
| post_id | title       | description                 | createdby | createdtime         |
+---------+-------------+-----------------------------+-----------+---------------------+
|       1 | hello       | the sun is shiningtoday lol |         2 | 2015-07-18 01:05:46 |
|       2 | hello again | its night now               |         2 | 2015-07-18 01:06:23 |
|       3 | wow         | just wow                    |         1 | 2015-07-11 01:06:55 |
|       4 | yeah        | hi there                    |         3 | 2015-07-18 01:37:48 |
|       5 | yeah2       | hithere for the 2nd time    |         3 | 2015-07-18 01:40:05 |
+---------+-------------+-----------------------------+-----------+---------------------+

+---------+------------------+------+--------+
| user_id | followin_user_id | date | status |
+---------+------------------+------+--------+
|       1 | NULL             | NULL | NULL   |
|       2 |                1 | NULL | NULL   |
|       3 |                1 | NULL | NULL   |
|       4 |                2 | NULL | NULL   |
|       5 |                2 | NULL | NULL   |
+---------+------------------+------+--------+

如果您使用的是 innodb,那么您可以像这样创建一个外键

ALTER TABLE `posts` ADD CONSTRAINT `posts_ibfk` FOREIGN KEY (`createdby`) REFERENCES `follow` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE; 

如果不使用 innodb 则不要介意最后一个命令:p

现在要获取关注另一个用户的用户的最后一篇文章,请使用以下查询

$qry = $this->db->query('
SELECT
*
FROM
posts
INNER JOIN follow ON posts.createdby = follow.user_id
WHERE
follow.followin_user_id = 1
ORDER BY
posts.createdtime DESC
');

return $qry->result();

结果应该是这样的

    +---------+-------------+-----------------------------+-----------+---------------------+---------+------------------+------+--------+
| post_id | title       | description                 | createdby | createdtime         | user_id | followin_user_id | date | status |
+---------+-------------+-----------------------------+-----------+---------------------+---------+------------------+------+--------+
|       5 | yeah2       | hithere for the 2nd time    |         3 | 2015-07-18 01:40:05 |       3 |                1 | NULL | NULL   |
|       4 | yeah        | hi there                    |         3 | 2015-07-18 01:37:48 |       3 |                1 | NULL | NULL   |
|       2 | hello again | its night now               |         2 | 2015-07-18 01:06:23 |       2 |                1 | NULL | NULL   |
|       1 | hello       | the sun is shiningtoday lol |         2 | 2015-07-18 01:05:46 |       2 |                1 | NULL | NULL   |
+---------+-------------+-----------------------------+-----------+---------------------+---------+------------------+------+--------+
4 rows in set

现在你所要做的就是获取第一行 user_id (createdby) 用 PHP 改变的地方

$user_id_flag=NULL;
foreach ($result as $row)
{
if($user_id_flag != $row->createdby)
    echo  $row->title;
else
    $user_id_flag=$row->createdby;
}

这将显示每个用户在 id = 1 的用户之后的最后一个帖子标题

希望对你有所启发

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2016-06-07
    • 2015-10-24
    • 2014-02-04
    相关资源
    最近更新 更多