如何将组行排序?

时间:2014-11-08 16:35:32

标签: mysql sql

这是我的问题:

SELECT * 
FROM `recordstouser` 
GROUP BY IdUserRecordstouser 
ORDER BY IdRecordstouser DESC;

在表格中有一个字段StatusRecordstouser,可以取值0/1。 因此,我需要按IdUserRecordstouser获取分组行,例如在分组行中将是StatusRecordstouser = 0;

的行

例如:

id | StatusRecordstouser | IdUserRecordstouser
1    1                     2
2    1                     2
3    0                     2
4    0                     3                     

输出2行:

4 | 0 | 3
3 | 0 | 2

最正确的版本:

SELECT * FROM ( SELECT * FROM `recordstouser` WHERE StatusRecordstouser = 0 ) t 
JOIN detailtousers ON detailtousers.idDetailToUsers = t.IdUserRecordstouser 
WHERE t.IdtoUserRecordstouser = 458 
GROUP BY t.IdUserRecordstouser ORDER BY t.id DESC

错误:

Unknown column 't.IdtoUserRecordstouser' in 'where clause'

4 个答案:

答案 0 :(得分:3)

组功能:使用MAX()MIN() MySQL。

第二种情况:

SELECT *
FROM (SELECT * FROM `recordstouser` GROUP BY IdUserRecordstouser) as data
ORDER BY IdRecordstouser;

答案 1 :(得分:3)

为什么需要group by?只需使用where

SELECT * 
FROM `recordstouser`
WHERE IdRecordstouser = 0;

EDTI:

因此,如果该行可用,您希望首选项为0。

您可以这样做:

SELECT *
FROM recordstouser rtu
WHERE IdRecordstouser = 0
UNION ALL
SELECT *
FROM recordstouser rtu
WHERE NOT EXISTS (SELECT 1
                  FROM recordstouser rtu2
                  WHERE rtu2.IdUserRecordstouser = rtu.IdUserRecordstouser AND rtu2.IdRecordstouser = 0
                 ) AND
      NOT EXISTS (SELECT 1
                  FROM recordstouser rtu2
                  WHERE rtu2.IdUserRecordstouser = rtu.IdUserRecordstouser AND rtu2.Id > rt.Id
                 );

这假定表中还有其他列。对于您的数据(及其合理的变体),这将起作用:

SELECT id, StatusRecordstouser, IdUserRecordstouser
FROM recordstouser rtu
WHERE IdRecordstouser = 0
UNION ALL
SELECT MAX(id), 1, IdUserRecordstouser
FROM recordstouser rtu
GROUP BY rtu.IdUserRecordstouser
HAVING SUM(IdRecordstouser = 0) = 0;

答案 2 :(得分:1)

试试这个。我知道你说你不能使用WHERE,但这有点不同。您可以在此处查看结果:http://sqlfiddle.com/#!2/8b5a0/11

SELECT *
FROM 
(
  SELECT * 
  FROM `recordstouser`
  WHERE StatusRecordstouser = 0
) t
GROUP BY t.IdUserRecordstouser
ORDER BY t.id DESC;

你也可以ORDER BY t.StatusRecordstouser ASC而不是ORDER BY t.id DESC来获得0。

答案 3 :(得分:1)

在我看来,您只想显示'statusrecordstouser'列为0的行(对于每个用户)但如果不存在,则显示'statusrecordstouser'为1的行。如果这是正确的,请考虑像这样的东西。

SELECT MAX(id), statusrecordstouser, iduserrecordstouser
FROM myTable
GROUP BY statusrecordstouser, iduserrecordstouser
ORDER BY iduserrecordstouser, statusrecordstouser;

如果用户有0和1,则返回两行,如果只有一行,则返回一行。我已经下令将它们按照你想要的顺序排列(0行总是先行)。如果一个组出现不止一次,我使用max来获取最新的行。

为了表明我的意思,这就是上述查询的输出:

| id | status | idUser |
+----+--------+--------+
| 3  |   0    |   2    |
+----+--------+--------+
| 2  |   1    |   2    |
+----+--------+--------+
| 4  |   0    |   3    |
+----+--------+--------+

现在,问题是如何区分这两行?好吧,你没必要,你想从每个分组中取出第一个,因为如果它是0你会接受它,如果没有0那么它将是1你也可以拿它。有关如何从组中获取第一项的信息,请参阅此question

查询看起来像这样:

SELECT t.id, t.statusrecordstouser, t.iduserrecordstouser
FROM(SELECT MAX(id) AS id, statusrecordstouser, iduserrecordstouser
    FROM myTable
    GROUP BY statusrecordstouser, iduserrecordstouser
    ORDER BY iduserrecordstouser, statusrecordstouser
    ) t
WHERE(SELECT COUNT(*)
      FROM(SELECT MAX(id) AS id, statusrecordstouser, iduserrecordstouser
          FROM myTable
          GROUP BY statusrecordstouser, iduserrecordstouser
          ORDER BY iduserrecordstouser, statusrecordstouser
      ) tt
      WHERE tt.iduserrecordstouser = t.iduserrecordstouser AND tt.statusrecordstouser <= t.statusrecordstouser
) <= 1;

我在SQL Fiddle中对此进行了测试,但确实有效。我甚至添加了一行只有状态1以确保它有效。