[英]How can I combine these two queries into one?
I am writing a script for my custom forums that determines if new threads or replies have been posted in a certain board since the user's last visit to that board(board_marks). 我正在为我的自定义论坛编写脚本,该脚本确定自用户上次访问该板以来,是否在某个板中发布了新主题或回复。 I have two tables: one that stores threads, another that stores replies.
我有两个表:一个表存储线程,另一个表存储回复。 I can easily find if there are any new replies in a board by using a left join.
我可以通过左联接轻松找到董事会中是否有任何新答复。 I want to add a bit that finds if there are new threads in that board.
我想添加一点,以发现该板上是否有新线程。 How would I do this?
我该怎么做?
My Tables: 我的桌子:
CREATE TABLE IF NOT EXISTS `forum_threads` (
`thread_id` int(15) NOT NULL AUTO_INCREMENT,
`board_id` int(15) NOT NULL,
`author_id` int(15) NOT NULL,
`updater_id` int(15) NOT NULL,
`title` text NOT NULL,
`content` text NOT NULL,
`date_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`views` int(15) NOT NULL,
`status` tinyint(1) NOT NULL,
`type` tinyint(1) NOT NULL COMMENT '0 normal, 1 sticky, 2 global.',
PRIMARY KEY (`thread_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `forum_messages` (
`message_id` int(15) NOT NULL AUTO_INCREMENT,
`thread_id` int(15) NOT NULL,
`author_id` int(15) NOT NULL,
`modifier_id` int(15) DEFAULT NULL,
`content` text NOT NULL,
`date_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date_modified` timestamp NULL DEFAULT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
My Script: 我的剧本:
$this->db->select('m.message_id, m.thread_id, m.date_posted, t.thread_id, t.board_id');
$this->db->from('forum_messages AS m');
$this->db->join('forum_threads AS t', 'm.thread_id = t.thread_id', 'left');
$this->db->where('t.board_id', $board_id);
$this->db->where('m.date_posted > "'.$last_mark['date_marked'].'"');
if($query_new_messages = $this->db->get()){
if($query_new_messages->num_rows() > 0){
$contains_new_posts = TRUE;
} else {
$contains_new_posts = FALSE;
}
}
The actual query: 实际查询:
SELECT
m.message_id, m.thread_id, m.date_posted, t.thread_id, t.board_id
FROM (forum_messages AS m)
LEFT JOIN forum_threads AS t ON m.thread_id = t.thread_id
WHERE `t`.`board_id` = '10' AND `m`.`date_posted` > "2014-03-02 06:01:31"
PS: I am doing this in CodeIgniter. PS:我在CodeIgniter中这样做。
An OR
condition will get you the data you want: OR
条件将为您提供所需的数据:
SELECT
m.message_id, m.thread_id, m.date_posted, t.thread_id, t.board_id
FROM (forum_messages AS m)
LEFT JOIN forum_threads AS t ON m.thread_id = t.thread_id
WHERE `t`.`board_id` = '10'
AND (`m`.`date_posted` > "2014-03-02 06:01:31"
OR `t`.`date_posted` > "2014-03-02 06:01:31")
You should benchmark this, however. 但是,您应该对此进行基准测试。 This solution is probably not very optimized.
该解决方案可能不是非常优化。 You should benchmark doing two fast queries - one for posts and one for threads - vs doing one potentially slower one.
您应该对两个快速查询进行基准测试-一个针对帖子,一个针对线程-与执行一个可能更慢的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.