2011-11-18 62 views
1

只是在寻找一种方式,以避免我的网站上重复后,这里是我的表的基本方案:合并两个表,看看日期

posts: 
+----+-------------------- + 
| ID | post_date   | 
+----+---------------------+ 
| 1 | 2011-11-15 08:42:50 | 
+----+---------------------+ 

meta: 
+--------------------+------------+ 
| post_id | meta_key | meta_value | 
+---------+----------+------------+ 
| 1 | ip |192.168.1.10| 
+---------+----------+------------+ 

我不知道如何构建查询,但基本是:

  • 由IP阻止,所以,我需要提供它。
  • 用户可以每隔300秒发帖。
  • 当用户被允许时返回空,否则返回其最新帖子的ID和发布日期。

回答

2

当用户被允许发布时,以下查询将返回1。第二个将返回用户的IP最后发布的ID和发布日期。

select 1 from post,meta 
    where meta.meta_value=<ip> 
    and post.id = meta.post_id 
    and post.post_date = max(select post_date from post,meta where meta.meta_value=<ip> and post.id = meta.post_id) 
    and post.post_date = (now()+300)); 

select post.id, post.post_date from post,meta 
    where meta.meta_value=<ip> 
    and post.id = meta.post_id 
    and post.post_date = max(select post_date from post,meta where meta.meta_value=<ip> and post.id = meta.post_id); 

你甚至可以使用IFNULL函数将它们合并,但你不能使用2列第二条语句是这种情况。

select ifnull(statement 1, statement 2);