编写mysql查询的想法

时间:2013-10-15 13:58:51

标签: mysql sql wordpress

我有一个名为wp_bp_activity的表格,其中包含许多列,我认为我应该针对此问题与其中4个列合作:idtypeitem_id和{ {1}}。

1 - 当有人发布新活动时,date_recordedtypeactivity_updateitem_id

2 - 当有人发布关于某个活动的新评论时,0typeactivity_commentitem_id中已存在活动ID

3 - 两者中,id是插入数据的日期。

表格中还有更多date_recorded

但是,我想只获取type type activity_update的行,最近有人回复是新的(我认为基于date_recorded

我试过的是:

SELECT  a.*, b.*
FROM wp_bp_activity as a, wp_bp_activity as b
WHERE
  ((b.type = 'activity_update') OR (b.type = 'activity_comment' AND b.item_id = a.id))
order by cast(a.date_recorded as datetime) desc
limit 0,20

执行时间太长,以内存不足错误结束。

对此类查询的任何帮助表示赞赏。

更新#1

                        wp_bp_activity table

    id              type             item_id         date_recorded
|---------|-----------------------|-----------|--------------------------
|  12081  |   activity_comment    |   12079   |    2013-10-18 07:27:01
|---------|-----------------------|-----------|--------------------------
|  12080  |   activity_update     |     0     |    2013-10-18 07:26:40
|---------|-----------------------|-----------|--------------------------
|  12079  |   activity_update     |     0     |    2013-10-17 05:15:43

此表中我想要的哪些行是此顺序的这些ID:

12079
12080

我不想得到的内容:

activity_comment类型

应该以哪种顺序获取行?

如您所见,activity_comment类型的行的item_id值为12079。所以12079是最近有人发表评论的最新活动,而12080没有评论,只是张贴了。所以我想要两个但是按照这个顺序:

12079
12080

4 个答案:

答案 0 :(得分:1)

您应该再添加一个字段

id, type, item_id, commented_on, date_recorded

::->> commented_on会证明item_idcommented_on的评论是合理的,这里12079评论为12079,因此您可以轻松获得所需内容

所以你的结构应该是这样的:

   id             type            commented_on   item_id         date_recorded
|---------|-----------------------|-----------|-----------|--------------------------
|  12081  |   activity_comment    |   12080   |   12079   |    2013-10-18 07:27:01
|---------|-----------------------|-----------|-----------|--------------------------
|  12080  |   activity_update     |     0     |     0     |    2013-10-18 07:26:40
|---------|-----------------------|-----------|-----------|--------------------------
|  12079  |   activity_update     |     0     |     0     |    2013-10-17 05:15:43

答案 1 :(得分:1)

也许我错过了一些巨大但又错误的东西?:

SELECT *
FROM `wp_bp_activity`
WHERE `type`='activity_update'
ORDER BY `date_recorded` ASC
LIMIT 0, 20;

答案 2 :(得分:1)

我将假设您正在寻找“最近的条目”(WHERE type = 'activity_update' AND date_recorded > [threshold])和“最近回复的条目,无论条目的年龄如何”(WHERE reply.type = 'activity_comment' AND reply.date_recorded > [threshold])。

第一组很简单:

SELECT entries.*
FROM activity AS entries
WHERE type = 'activity_update' AND date_recorded > [threshold]

第二组有点不太明显:

SELECT entries.*
FROM activity AS entries
JOIN activity AS replies
    ON replies.item_id = entries.id
    AND replies.type = 'activity_comment'
WHERE
    entries.type = 'activity_update'
    AND replies.date_recorded > [threshold]

全部放在一起:

SELECT entries.*
FROM activity AS entries
LEFT JOIN activity AS replies -- LEFT JOIN, because an INNER JOIN would filter out entries without any reply
    ON  replies.item_id = entries.id
    AND replies.type = 'activity_comment'
WHERE
    entries.type = 'activity_update'
    AND (
        entries.date_recorded > [threshold]
        OR replies.date_recorded > [threshold] -- evaluates as FALSE if replies.date_recorded is NULL
    )
ORDER BY IFNULL(replies.date_recorded, entries.date_recorded) -- replies if not null, entries otherwise

我并不为我表现不佳的ORDER BY条款感到自豪,我希望有人能提出更好的主意

答案 3 :(得分:0)

您在OR子句中有JOIN条件。这是您不应该使用该语法的一个很好的例子。

SELECT  a.*, b.*
FROM wp_bp_activity as a JOIN wp_bp_activity as b ON b.item_id = a.id
WHERE
  ((b.type = 'activity_update') OR (b.type = 'activity_comment'))
order by cast(a.date_recorded as datetime) desc
limit 0,20