1

我正在尝试在 sqlite3 的 group_concat 中订购。mysql 不支持这种方式,所以我不能这样做:

group_concat(logID order by logDateTime DESC)

我的完整查询在这里,我需要按 logDateTime 排序 logID。我在这里读到了一个子查询方法:Sqlite group_concat ordering,但我无法让它在我的查询中工作。

SELECT logID,
       NULL AS sessionID,
       logDateTime,
       NULL AS sessionName,
       NULL AS NamesInRandomOrder
FROM logs
WHERE sessionID IS NULL

UNION ALL

SELECT NULL,
       sessions.sessionID,
       MAX(logDateTime),
       sessions.sessionName,
       group_concat(logID)
FROM logs
JOIN sessions ON logs.sessionID = sessions.sessionID
GROUP BY sessions.sessionID

ORDER BY 3 DESC
4

1 回答 1

1

这对子查询进行了排序,但 SQLite 不保证在所有情况下都保持该顺序:

SELECT logID,
       NULL AS sessionID,
       logDateTime,
       logName,
       NULL AS NamesInRandomOrder
FROM logs
WHERE sessionID IS NULL

UNION ALL

SELECT NULL,
       sessionID,
       MAX(logDateTime),
       sessionName,
       group_concat(logName)
FROM (SELECT sessions.sessionID AS sessionID,
             logDateTime,
             sessionName,
             logName
      FROM logs JOIN sessions ON logs.sessionID = sessions.sessionID
      ORDER BY sessions.sessionID,
               logName)
GROUP BY sessionID

ORDER BY 3 DESC
于 2013-02-20T10:21:49.037 回答