合并两个表并查看日期

时间:2011-11-18 16:16:38

标签: mysql

只是想办法避免在我的网站上重复发帖,这是我的表格的基本方案:

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和发布日期。

1 个答案:

答案 0 :(得分: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);