Android 房间:查询未返回所有带连接的行

Android Room: Query not returning all rows with a join

我有两个 tables - table a 和 table b。在 table a 中,我有 20 列,其中 10 列连接到 table b。这样做的原因是用户可以 select 无法将多个值存储到 table 中的一个 cell/row/column 中,因此需要将其标准化为自己的 table。我在检索数据时遇到问题。如果数据同时存在于 table a 和 b 中,则查询仅使用 returns 结果。但是我的用例允许用户只在 table 中拥有数据。我需要将查询修改为 return 行中存在的任何数据,无论 table b.

中是否没有数据

这是我的查询的简化版本(不需要全部 20 列)

SELECT
tableA.id,
tableA.currentSituation,
tableA.moodStart,
tableA.automaticThought1,
tableB.id,
tableB.allOrNothing,
tableB.blamingOthers,
tableB.catastrophizing,
FROM tableA
JOIN tableB
ON tableA.id = tableB.tableAid
ORDER BY tableA.currentSituation
DESC

任何将此查询修改为 return 所有 rows/data 存在的帮助,无论 table b 中是否存在数据,我们将不胜感激。

你好吗?我看到两个错误来更正您的脚本:首先在您的脚本中添加“SELECT”字样,然后将“JOIN”更改为“LEFT OUTER JOIN”:

SELECT tableA.id,
tableA.currentSituation,
tableA.moodStart,
tableA.automaticThought1,
tableB.id,
tableB.allOrNothing,
tableB.blamingOthers,
tableB.catastrophizing,
FROM tableA
LEFT JOIN tableB
ON tableA.id = tableB.tableAid
ORDER BY tableA.currentSituation
DESC;

更多信息:sqlite joins reference

The LEFT JOIN keyword returns all records from the left table [tableA], and the matched records from the right table [tableB]. The result is NULL from the right side, if there is no match.

来自 https://www.w3schools.com/sql/sql_join_left.asp

我认为 LEFT JOIN 是您想要的。但是您需要检查是否为 null,因为 tableB 条目可能不存在。