简体   繁体   English

获取最大值和最小值以及所有值和多个where子句

[英]Get max and min values along with all values and multiple where clause

I have this query which works very good 我有这个查询很好

  SELECT cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown 
  FROM chat_messages cm 
  WHERE TIMESTAMPDIFF(SECOND,cm.date_edited,'$now') < 10 GROUP BY cm.id

Which gives me the entries which are edited in less then 10 seconds. 这给了我不到10秒即可编辑的条目。

But along with that im trying to get also the max(voteup) and min(votedown) 但是max(voteup)我试图获得max(voteup)min(votedown)

But dont affect the first entries of the first query . 但是不要影响第一个查询的第一个条目。 How can i combine then to get all entries i need ? 然后如何合并才能获得所需的所有条目?

Example: 例:

if im getting 3 newly updates entries . 如果我得到3新更新条目。 i want to get them those 3 plus the max of voteup and votedown . 我想让他们得到那3个加上投票和投票最多的票数。

Example: 例:

    id   edited  date_edited          voteup    votedown
    37      0      2016-03-05 22:13:03    5         0
    38      0      2016-04-02 11:15:00    3         7
    39      0      2016-03-05 22:10:06    10        6
    40      0      2016-03-20 21:40:06    5         0
    41      1      2016-04-20 22:28:59    5         0
    42      1      2016-03-20 21:59:15    0         20
    43      1      2016-04-21 22:20:25    8         0     <---- this new updated

My wished result is 我希望的结果是

    id   edited  date_edited         voteup  votedown  maxup  maxdown
    39      0      2016-03-05 22:10:06    10        6    10     NULL
    42      1      2016-03-20 21:59:15    0         20   NUll   20
    43      1      2016-04-21 22:20:25    8         0    NULL   NULL  

My $now time is 2016-04-21 22:20:20 $now时间是2016-04-21 22:20:20

explanation: 说明:

   -id 39 is having maxup vote i want get it

   -id 42  is having maxdown i want get it

   -id 43 is newly updated in that period of 10 seconds.

so i general i want get new updated entries pls the max up and down . 所以我一般我想获得新的更新条目,请最大和最大。

if many max voteup values are same then just choose one which have min votedown 如果许多最大投票值相同,则只需选择一个具有最小投票率的值

Any solution to that pls ? 有什么解决办法吗?

Here my sqlfiddle example 这是我的sqlfiddle示例

Edit: oh sorry i meant id . 编辑:抱歉,我的意思是id。 now wish my question is clear like 现在希望我的问题很清楚 在此处输入图片说明

you will want to use a UNION statement: 您将要使用UNION语句:

SELECT * FROM (
    select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown 
        , voteup as maxup, null AS maxdown
    from chat_messages cm
    ORDER BY voteup DESC, votedown
    LIMIT 1
) a
UNION
SELECT * FROM (
    select cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown 
        , null as maxup, votedown AS maxdown
    from chat_messages cm
    ORDER BY votedown DESC, voteup
    LIMIT 1
) b
UNION
SELECT * FROM (
  SELECT cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown 
        , null as maxup, null AS maxdown
  from chat_messages cm
  WHERE TIMESTAMPDIFF(SECOND,cm.date_edited,'2016-04-21 22:20:20') < 10
) c

note that I used '2016-04-21 22:20:20' , but you will want to substitute the $now back in 请注意,我使用了'2016-04-21 22:20:20' ,但是您将想用$now代替

you can use user-defined variables to track your maximums and then outer query the rows that match your rules. 您可以使用用户定义的变量来跟踪最大值,然后外部查询与规则匹配的行。

SELECT id,edited,date_edited,voteup,votedown,
       IF(voteup=@maxvoteup,voteup,NULL) as maxvoteup,
       IF(votedown=@maxvotedown,votedown,NULL) as maxvotedown
FROM (SELECT cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown,
       @maxvoteup := GREATEST(@maxvoteup,cm.voteup) as maxvoteup,
       @maxvotedown := GREATEST(@maxvotedown,cm.votedown) as maxvotedown
      FROM chat_messages cm,(SELECT @maxvoteup:=0,@maxvotedown:=0)initial
     )T
WHERE TIMESTAMPDIFF(SECOND,date_edited,'2016-04-21 22:20:25') < 10
   OR voteup = @maxvoteup
   OR votedown = @maxvotedown
ORDER BY id ASC

sqlfiddle sqlfiddle

Here's another query that is much more CRAZY..but it works .. 这是另一个更疯狂的查询..但它有效..

for maxupvote row, it'll find row that has maxupvote and minimum down vote, if more than 1 row exists (in a tie) it'll grab the row with latest/largest id. 对于maxupvote行,它将找到具有maxupvote和最小否定表决权的行,如果存在多于1行(并列),则它将获取ID为最新/最大的行。 for maxdownvote row, it'll find row that has maxdownvote and minimum up vote, if more than 1 row exists (in a tie) it'll grab the row with latest/largest id. 对于maxdownvote行,它将找到具有maxdownvote和最小投票数的行,如果存在多于1行(并列),则它将获取具有最新/最大id的行。

SELECT id,edited,date_edited,voteup,votedown,
       IF(voteup=@maxvoteup,voteup,NULL) as maxvoteup,
       IF(votedown=@maxvotedown,votedown,NULL) as maxvotedown
FROM (SELECT cm.id ,cm.edited,cm.date_edited,cm.voteup,cm.votedown,
       @minvotedown :=
            (CASE WHEN cm.voteup > @maxvoteup OR (cm.voteup = @maxvoteup AND cm.votedown < @minvotedown)
                  THEN cm.votedown
                  ELSE @minvotedown
            END),
       @minvoteup :=
            (CASE WHEN cm.votedown > @maxvotedown OR (cm.votedown = @maxvotedown AND cm.voteup < @minvoteup)
                  THEN cm.voteup
                  ELSE @minvoteup
            END),
       @maxvoteup := GREATEST(@maxvoteup,cm.voteup) as maxvoteup,
       @maxvotedown := GREATEST(@maxvotedown,cm.votedown) as maxvotedown,
       @maxvoteupid :=
             (CASE WHEN cm.voteup = @maxvoteup AND cm.votedown = @minvotedown 
              THEN cm.id
              ELSE @maxvoteupid
              END),
       @maxvotedownid :=
             (CASE WHEN cm.votedown = @maxvotedown AND cm.voteup = @minvoteup
              THEN cm.id
              ELSE @maxvotedownid
              END)
      FROM chat_messages cm,(SELECT @maxvoteup:=0,@maxvotedown:=0,@minvoteup:=0,@minvotedown:=0,@maxvoteupid:=0,@maxvotedownid:=0)initial
      ORDER BY cm.id ASC
     )T
WHERE TIMESTAMPDIFF(SECOND,date_edited,'2016-04-21 22:20:25') < 10
   OR id = @maxvoteupid
   OR id = @maxvotedownid
ORDER BY id ASC;

sqlfiddle sqlfiddle

I call it CRAZY because it is...if i was doing this. 我称它为CRAZY,因为它是...如果我正在这样做。 I would just run 3 separate queries 我只需要运行3个单独的查询

1 query to return one row with order by upvote DESC, downvote ASC, id DESC limit 1 1个查询以order by upvote DESC, downvote ASC, id DESC限制1的order by upvote DESC, downvote ASC, id DESC返回一行

1 query to return one row with order by downvote DESC, upvote ASC, id DESC limit 1 1个查询以order by downvote DESC, upvote ASC, id DESC限制1的order by downvote DESC, upvote ASC, id DESC返回一行

1 query to return one row that's within the last 10 seconds order by id DESC this way it's much easier to maintain. 1个查询可返回order by id DESC的最后10秒内的一行,这样更易​​于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM