SQL组按输出特定数据

时间:2016-09-12 03:37:15

标签: mysql sql

Construct(T const& )

我想按规则输出

ID     tag      index 
001       1     fsklgg
001       2     segwrh
001       3     esfjkg
002       4     seggrg
002       5     gehewv
002       6     egwgsg
003       1     esgges
003       4     yjkdsa
003       9     wrfsbb
像这样

1. each ID will only have one output
2. the output will be  sort by "tag" which is the largest

但我的回答总是

ID     tag      index 
001       3     esfjkg
002       6     egwgsg
003       9     wrfsbb

我使用GROUP BY标签,但答案是最小的。 我试图使用ORDER BY DESC,希望答案会改变,但它不起作用。 任何人都可以教我如何处理答案吗? 或者我应该使用什么命令?

我的源代码是

ID     tag      index 
001       1     fsklgg
002       4     seggrg
003       1     esgges

使用MariaDB

2 个答案:

答案 0 :(得分:2)

//Pressed start
I/System.out: Starting counter!
I/System.out: RUNNING
I/System.out: 0
I/System.out: 1
I/System.out: 2
I/System.out: 3
I/System.out: 4
I/System.out: 5
I/System.out: 6
//Pressed stop
I/System.out: Stopping counter!
I/System.out: PENDING
//Pressed start
I/System.out: Starting counter!
I/System.out: RUNNING
//Pressed stop
I/System.out: Stopping counter!
I/System.out: PENDING

使用子查询:

SELECT t1.ID,
       t1.tag,
       t1.index
FROM yourTable t1
INNER JOIN
(
    SELECT ID, MAX(tag) AS tag
    FROM yourTable
    GROUP BY ID
) t2
    ON t1.ID  = t2.ID AND
       t1.tag = t2.tag
ORDER BY t1.tag

答案 1 :(得分:1)

试试这个:

SELECT a.* 
FROM tableA a
WHERE EXISTS(
     SELECT ID, tag FROM (
              SELECT ID, MAX(tag) tag FROM tableA GROUP BY ID) t 
WHERE t.id = a.id AND t.tag = a.tag) 
ORDER BY a.tag DESC