按ID分组,按日期排序,组ID为Drupal

时间:2016-07-19 08:17:28

标签: mysql drupal drupal-7

我正在使用drupal,我有nodeaccess模块​​。我有一个单一用户的角色。我已经为单个节点授予了许多角色。

我需要输出如下所示。

a)按日期排序(首选)
b)在日期顺序中,我需要按gid分组

实施例

我写了一个查询

SELECT n.nid, na.gid, UNIX_TIMESTAMP(fav.field_date_posted_value), fav.field_date_posted_value
FROM node as n
JOIN nodeaccess AS na ON na.nid = n.nid 
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
ORDER BY fav.field_date_posted_value DESC, n.nid ASC
LIMIT 10

我的结果如下

+-------+-----+-------------------------+
| nid   | gid | field_date_posted_value |
+-------+-----+-------------------------+
| 12501 |   1 | 2014-12-07              |
| 12501 |  10 | 2014-12-07              |
| 12502 |   1 | 2014-12-07              |
| 12502 |   3 | 2014-12-07              |
| 12502 |   8 | 2014-12-07              |
| 12502 |  10 | 2014-12-07              |
| 12502 |  11 | 2014-12-07              |
| 12505 |   1 | 2014-05-05              |
| 12505 |  10 | 2014-05-05              |
| 12575 |   1 | 2014-12-24              |
| 12575 |  10 | 2014-12-24              |
| 12576 |   1 | 2013-05-26              |
| 12576 |  10 | 2013-05-26              |
| 12577 |   1 | 2013-05-14              |
| 12577 |  10 | 2013-05-14              |
+-------+-----+-------------------------+

但是当我尝试按下面的nid进行分组时

SELECT n.nid, na.gid, UNIX_TIMESTAMP(fav.field_date_posted_value), fav.field_date_posted_value
FROM node as n
JOIN nodeaccess AS na ON na.nid = n.nid 
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
GROUP BY n.nid ORDER BY fav.field_date_posted_value DESC, n.nid ASC
LIMIT 10

结果

+-------+-----+-------------------------+
| nid   | gid | field_date_posted_value |
+-------+-----+-------------------------+
| 12501 |  01 | 2014-12-24              |
| 12502 |  11 | 2014-12-07              |
| 12505 |  01 | 2014-12-07              |
| 12575 |  01 | 2014-05-26              |
| 12576 |  01 | 2013-05-14              |
| 12577 |  01 | 2013-05-05              |
+-------+-----+-------------------------+

但我的预期结果是

+-------+-----+-------------------------+
| nid   | gid | field_date_posted_value |
+-------+-----+-------------------------+
| 12501 |  10 | 2014-12-24              |
| 12502 |  11 | 2014-12-07              |
| 12505 |  10 | 2014-12-07              |
| 12575 |  10 | 2014-05-26              |
| 12576 |  10 | 2013-05-14              |
| 12577 |  10 | 2013-05-05              |
+-------+-----+-------------------------+

有没有办法做到这一点?

我想将结果重新排序为 group by nid 按日期排序(DESC)按gid排序(DESC)

1 个答案:

答案 0 :(得分:1)

您可以尝试在gidfield_date_posted_value列上使用汇总函数,这是您应该在正确的GROUP BY查询中执行的操作:

SELECT n.nid,
       MAX(na.gid),
       MAX(fav.field_date_posted_value)
FROM node AS n
JOIN nodeaccess AS na
    ON na.nid = n.nid 
LEFT JOIN field_data_field_date_posted AS fav
    ON fav.entity_id = n.nid
GROUP BY n.nid
ORDER BY fav.field_date_posted_value DESC,
         n.nid ASC
LIMIT 10

实际上,由于nid列似乎与field_date_posted_value列完全相关,因此您可以对这两列进行分组,即使用此列:

SELECT n.nid, MAX(na.gid), fav.field_date_posted_value
FROM ...
...
GROUP BY n.nid, fav.field_date_posted_value