MYSQLI-当我使用union all和group by时获取所有最后一个id

时间:2018-09-24 07:57:22

标签: mysql sql mysqli

如何更改以下代码以获得所有三元组(nr1,n2,n3)的最大ID? 每组三元组出现几次(几个id)。我想获取每个三元组的最大ID。例如:

test.wa-essence.com/wachampionacademy

answer(前两行(按计数顺序)):

id    Number1   Number2   Number3   Number4   Number5                                                                       
66       2         3         4         7         9
74       2         3         5         6         8                        
102      3         7         8         9        10
123      3         6         7         9        10
157      1         2         3         4         5   
188      1         2         3         5         7 

我试图在select和中添加id,但结果不完整。请帮忙。

nr1   nr2   nr3   count   max_id                                            
 2     3     5      3       188
 3     7     9      3       123

2 个答案:

答案 0 :(得分:1)

DESC顺序中的最后一个是ASC中的第一个,因此您可以将限制1用于ASC

    select nr1,nr2,nr3, count(*)
    from         (select Number1 as nr1, Number2 as nr2, Number3 as nr3 from table
                  union all
                  select Number1 as nr1, Number2 as nr2, Number4 as nr3 from table
                  union all
                  select Number1 as nr1, Number2 as nr2, Number5 as nr3 from table
                  union all
                  select Number1 as nr1, Number3 as nr2, Number4 as nr3 from table
                  union all
                  select Number1 as nr1, Number3 as nr2, Number5 as nr3 from table
                  union all
                  select Number1 as nr1, Number4 as nr2, Number5 as nr3 from table
                  union all
                  select Number2 as nr1, Number3 as nr2, Number4 as nr3 from table
                  union all
                  select Number2 as nr1, Number3 as nr2, Number5 as nr3 from table
                  union all
                  select Number2 as nr1, Number4 as nr2, Number5 as nr3 from table
                  union all
                  select Number3 as nr1, Number4 as nr2, Number5 as nr3 from table) g
    group by nr1, nr2, nr3
    ORDER BY count(*) ASC, nr1, nr2, nr3
    LIMIT 1

答案 1 :(得分:0)

您应该修复数据结构。每个ID和每个数字都最好排成一行。

这看起来很复杂。在一种不同寻常的情况下,我将从视图开始(MySQL不支持CTE):

create view better_data as
    select id, number1 as num, 1 as which from tab union all
    select id, number2 as num, 2 as which from tab union all
    select id, number3 as num, 3 as which from tab union all
    select id, number4 as num, 4 as which from tab union all
    select id, number5 as num, 5 as which from tab;

然后,您可以使用以下命令生成所有三元组:

select distinct bd1.num, bd2.num, bd3.num
from better_data bd1 cross join
     better_data bd2 cross join
     better_data bd3
where bd1.num < bd2.num and bd2.num < bd3.num;

现在,您可以通过汇总来计算:

select triples.num1, triples.num2, triples.num3, count(*) as cnt
from (select distinct bd1.num as num1, bd2.num as num2, bd3.num as num3
      from better_data bd1 cross join
           better_data bd2 cross join
           better_data bd3
      where bd1.num < bd2.num and bd2.num < bd3.num
     ) triples left join
     t
     on triples.num1 in (tab.number1, tab.number2, tab.number3, tab.number4, tab.number5) and
        triples.num2 in (tab.number1, tab.number2, tab.number3, tab.number4, tab.number5) and
        triples.num3 in (tab.number1, tab.number2, tab.number3, tab.number4, tab.number5)
group by triples.num1, triples.num2, triples.num3
order by count(*) desc;