Sqlite Android顺序由& GROUP_CONCAT

时间:2015-04-21 07:27:58

标签: android sqlite sql-order-by group-concat

我使用sqlite3:3.8.2与sqliteman(Ububuntu 14.04)在我的程序Android中尝试我需要的查询。

Android版4.0 +。

我有3个表用来获取数据

我发现了这个问题:

当我在sqliteman

中使用此查询时
SELECT st.nome
AS nome,
st.cognome
AS cognome, 
(SELECT ae.usercode
AS usercode
FROM assenze ae
WHERE st.ultimo_evento = ae.fk_evento)
AS ultimo_evento,
(SELECT 
    GROUP_CONCAT(
        ev.nome_evento, ", "
    )
    AS nome_evento 
    FROM eventi ev 
    WHERE ev.studente_id = st.id 
)
AS nome_evento
FROM studenti st 
WHERE st.classe_ident ='4297'
ORDER BY st.cognome

输出是:

EP, USC, R1, R, A ... 对我来说很完美

但是当我在android db.rawQuery()中使用它时,结果是

A, EP, R, R1, USC ... 对我没有好处

似乎也与此查询的结果相同(在sqliteman和android中执行):

SELECT 
st.nome AS nome, 
st.cognome AS cognome, 
ae.usercode AS ultimo_evento, 
GROUP_CONCAT(
   ev.nome_evento, ", "
) 
AS nome_evento
FROM studenti st
LEFT JOIN eventi ev
ON st.id = ev.studente_id
LEFT JOIN assenze ae
ON st.ultimo_evento = ae.fk_evento
WHERE st.classe_ident ='4297'
GROUP BY st.id
ORDER BY st.cognome

可能这是我的错,但我仍在努力寻找解决方案...

由于

似乎没有ORDER BY ev.data_ora_evento也可以。但在Android中仍然没有工作......

1 个答案:

答案 0 :(得分:0)

group_concat() documentation说:

  

连接元素的顺序是任意的。

但是,您可以尝试通过对已排序的子查询执行group_concat()来强制执行命令:

SELECT GROUP_CONCAT(ev.nome_evento, ", ") AS nome_evento 
FROM (SELECT nome_evento
      FROM eventi
      WHERE studente_id = st.id
      ORDER BY whatever) AS ev