<legend id='JFITV'><style id='JFITV'><dir id='JFITV'><q id='JFITV'></q></dir></style></legend>
    • <bdo id='JFITV'></bdo><ul id='JFITV'></ul>

    <tfoot id='JFITV'></tfoot>
      <i id='JFITV'><tr id='JFITV'><dt id='JFITV'><q id='JFITV'><span id='JFITV'><b id='JFITV'><form id='JFITV'><ins id='JFITV'></ins><ul id='JFITV'></ul><sub id='JFITV'></sub></form><legend id='JFITV'></legend><bdo id='JFITV'><pre id='JFITV'><center id='JFITV'></center></pre></bdo></b><th id='JFITV'></th></span></q></dt></tr></i><div id='JFITV'><tfoot id='JFITV'></tfoot><dl id='JFITV'><fieldset id='JFITV'></fieldset></dl></div>

      <small id='JFITV'></small><noframes id='JFITV'>

        编写 mysql 查询的想法

        时间:2023-06-06

            <legend id='4do0s'><style id='4do0s'><dir id='4do0s'><q id='4do0s'></q></dir></style></legend>
          1. <small id='4do0s'></small><noframes id='4do0s'>

            <i id='4do0s'><tr id='4do0s'><dt id='4do0s'><q id='4do0s'><span id='4do0s'><b id='4do0s'><form id='4do0s'><ins id='4do0s'></ins><ul id='4do0s'></ul><sub id='4do0s'></sub></form><legend id='4do0s'></legend><bdo id='4do0s'><pre id='4do0s'><center id='4do0s'></center></pre></bdo></b><th id='4do0s'></th></span></q></dt></tr></i><div id='4do0s'><tfoot id='4do0s'></tfoot><dl id='4do0s'><fieldset id='4do0s'></fieldset></dl></div>

          2. <tfoot id='4do0s'></tfoot>
                <bdo id='4do0s'></bdo><ul id='4do0s'></ul>

                    <tbody id='4do0s'></tbody>
                  本文介绍了编写 mysql 查询的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个名为 wp_bp_activity 的表,其中包含许多列,我认为我应该使用其中的 4 个来解决这个问题:id, typeitem_iddate_recorded.

                  I've a table named wp_bp_activity with many columns that I think I should work with 4 of them for this issue: id, type, item_id and date_recorded.

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

                  1 - When someone post a new activity the type is activity_update and item_id is 0

                  2 - 当有人对活动发表新评论时,其 typeactivity_comment 并且 item_id 存在id

                  2 - When someone post a new comment on an activity its type is activity_comment and item_id is existed activity id in column id

                  3 - 在两者中,date_recorded 是插入数据的日期.

                  3 - In both, date_recorded is the date of when data is inserted.

                  表中还有更多type.

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

                  But I wanna fetch only rows with type of activity_update that someone is recently replied to or are new (based on date_recorded I think)

                  我试过的是:

                  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
                  

                  执行时间过长,并以内存不足错误结束.

                  That takes too long to be executed and ends with memory insufficient error.

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

                  Any help on this kind of query is appreciated.

                  更新 #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没有评论只是发布.所以我想要两个,但按这个顺序:

                  As you can see the row with activity_comment type has an item_id with the value of 12079. so 12079 is the latest activity that recently someone made a comment on it, and 12080 has no comments but is just posted. So I want both but at this order:

                  12079
                  12080
                  

                  推荐答案

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

                  I am going to assume that you are looking for "recent entries" (WHERE type = 'activity_update' AND date_recorded > [threshold]) and "entries having a recent reply, regardless of the entry's age" (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 子句表现不佳而自豪,我希望有人能提出更好的想法

                  I am not proud of my poorly performing ORDER BY clause, I hope someone can suggest a better idea

                  这篇关于编写 mysql 查询的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mySQL 查询键值对 下一篇:更新后恢复 MySQL/var/lib/mysql

                  相关文章

                  最新文章

                  <small id='YGeTh'></small><noframes id='YGeTh'>

                  <tfoot id='YGeTh'></tfoot>
                1. <i id='YGeTh'><tr id='YGeTh'><dt id='YGeTh'><q id='YGeTh'><span id='YGeTh'><b id='YGeTh'><form id='YGeTh'><ins id='YGeTh'></ins><ul id='YGeTh'></ul><sub id='YGeTh'></sub></form><legend id='YGeTh'></legend><bdo id='YGeTh'><pre id='YGeTh'><center id='YGeTh'></center></pre></bdo></b><th id='YGeTh'></th></span></q></dt></tr></i><div id='YGeTh'><tfoot id='YGeTh'></tfoot><dl id='YGeTh'><fieldset id='YGeTh'></fieldset></dl></div>

                  <legend id='YGeTh'><style id='YGeTh'><dir id='YGeTh'><q id='YGeTh'></q></dir></style></legend>
                    <bdo id='YGeTh'></bdo><ul id='YGeTh'></ul>