如何编写查询以获得mysql中的结果

时间:2015-07-17 18:41:46

标签: php mysql codeigniter

我有两个mysql表帖子,并按照以下内容包含followid,from_user_id,to_user_id,日期,状态和帖子包含标题,描述,createdby,createdtime 我想写一个查询来获取最新添加的帖子,该帖子由跟随特定用户的用户发布。这意味着只是我们的Facebook页面,发布我们的朋友发布到我们页面的帖子。可以立即给任何人提供帮助。

1 个答案:

答案 0 :(得分:0)

好的,我改变了你的架构,看起来像这样:

关注表格

+------------------+----------+------+-----+---------+-------+
| 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

用户的每个用户的最后一个帖子标题

希望激励你

喝彩!