使用带有复合主键的内部连接删除
Delete using inner join with composite primary key
我正在尝试从我的数据库中删除一些记录,我已经成功了并且在“DB Browser for SQLite”软件中测试时它可以工作但是当我尝试将它复制到我的 Android Studio 项目时我得到我的 DAO 中出现这个错误:
'(',')', '.', BETWEEN or IN expected, got ','
这是我的代码:
@Query("DELETE FROM AsistenciaEstatus\n" +
"WHERE (id_persona_ae, id_registro_asistencia_ae) IN(\n" +
"SELECT id_persona_ae, id_registro_asistencia_ae FROM AsistenciaEstatus\n" +
"INNER JOIN RegistroAsistencia ON AsistenciaEstatus.id_registro_asistencia_ae=RegistroAsistencia._id\n" +
"WHERE AsistenciaEstatus.id_persona_ae=:idPersona\n" +
"AND RegistroAsistencia.id_grupo_ra=:idGrupo)")
void borrarTodosLosEstatusDePersonaEnXGrupo(long idPersona, long idGrupo);
错误出现在第二行的逗号处
WHERE (id_persona_ae, id_registro_asistencia_ae)
我不明白可能是什么问题以及如何解决
ROW VALUES 的使用在 SQLite 3.15.0 版本中引入,API Level 26+ 在 Android.
[=18 中支持=]
如果您的应用针对较低的 API 级别,您可以在查询中使用 EXISTS
:
DELETE FROM AsistenciaEstatus AS a
WHERE a.id_persona_ae = :idPersona
AND EXISTS (
SELECT 1
FROM RegistroAsistencia AS r
WHERE a.id_registro_asistencia_ae = r._id
AND r.id_grupo_ra = :idGrupo
);
或者,由于您使用参数 :idPersona
过滤 id_persona_ae
,您可以在 WHERE
子句中单独检查它,并让 IN
子查询到 return 只有 1 列:
DELETE FROM AsistenciaEstatus
WHERE id_persona_ae = :idPersona
AND id_registro_asistencia_ae IN (
SELECT a.id_registro_asistencia_ae
FROM AsistenciaEstatus AS a INNER JOIN RegistroAsistencia AS r
ON a.id_registro_asistencia_ae = r._id
WHERE a.id_persona_ae = :idPersona
AND r.id_grupo_ra = :idGrupo
);
我正在尝试从我的数据库中删除一些记录,我已经成功了并且在“DB Browser for SQLite”软件中测试时它可以工作但是当我尝试将它复制到我的 Android Studio 项目时我得到我的 DAO 中出现这个错误:
'(',')', '.', BETWEEN or IN expected, got ','
这是我的代码:
@Query("DELETE FROM AsistenciaEstatus\n" +
"WHERE (id_persona_ae, id_registro_asistencia_ae) IN(\n" +
"SELECT id_persona_ae, id_registro_asistencia_ae FROM AsistenciaEstatus\n" +
"INNER JOIN RegistroAsistencia ON AsistenciaEstatus.id_registro_asistencia_ae=RegistroAsistencia._id\n" +
"WHERE AsistenciaEstatus.id_persona_ae=:idPersona\n" +
"AND RegistroAsistencia.id_grupo_ra=:idGrupo)")
void borrarTodosLosEstatusDePersonaEnXGrupo(long idPersona, long idGrupo);
错误出现在第二行的逗号处
WHERE (id_persona_ae, id_registro_asistencia_ae)
我不明白可能是什么问题以及如何解决
ROW VALUES 的使用在 SQLite 3.15.0 版本中引入,API Level 26+ 在 Android.
[=18 中支持=]
如果您的应用针对较低的 API 级别,您可以在查询中使用 EXISTS
:
DELETE FROM AsistenciaEstatus AS a
WHERE a.id_persona_ae = :idPersona
AND EXISTS (
SELECT 1
FROM RegistroAsistencia AS r
WHERE a.id_registro_asistencia_ae = r._id
AND r.id_grupo_ra = :idGrupo
);
或者,由于您使用参数 :idPersona
过滤 id_persona_ae
,您可以在 WHERE
子句中单独检查它,并让 IN
子查询到 return 只有 1 列:
DELETE FROM AsistenciaEstatus
WHERE id_persona_ae = :idPersona
AND id_registro_asistencia_ae IN (
SELECT a.id_registro_asistencia_ae
FROM AsistenciaEstatus AS a INNER JOIN RegistroAsistencia AS r
ON a.id_registro_asistencia_ae = r._id
WHERE a.id_persona_ae = :idPersona
AND r.id_grupo_ra = :idGrupo
);