我准备了一个左外连接查询,当我的 BLOB 表中存在与我的 INCIDENT 表中的记录匹配键的记录时,该查询应该返回非空数据。
基本上,数据和查询看起来像这样:
Table A Key Table B Key Blob
and the query I'm running, should show all records from A, and the 'hasb' column should be 1 if there's a record in B with a matching key column, or 0 if there isn't. So the query should look like this:
SELECT A.*, ifnull(A.Key = B.Key, 0) as hasb FROM A
LEFT OUTER JOIN B ON A.Key = B.Key
好的,所以我的问题是,除了在 Android 设备上,这个查询似乎在我尝试使用它的任何地方都有效。
这是一个带有实际表和查询的 SQLFiddle。请注意,查询在那里有效。
http://sqlfiddle.com/#!7/89e7d/4
有人知道为什么这在 Android 上不起作用吗?我正在测试的设备是运行 Android 4.1.1 的三星 Galaxy S 3。
最佳答案
我认为您使用 ifnull
使事情过于复杂。
这是你的 fiddle 中的查询:
SELECT ifnull( a.userid = b.userid, 0 ) FROM incidentdata AS a
LEFT OUTER JOIN incidentblob AS b
ON ( a.userid = b.userid ) AND ( a.incidenttag = b.incidenttag );
这是我要写的查询:
SELECT a.userid, a.incidenttag, b._id, b._id is not null hasb
FROM incidentdata AS a
LEFT JOIN incidentblob AS b
ON a.userid = b.userid AND a.incidenttag = b.incidenttag
有用吗?如果不满意,请提供您正在使用的 SQLite 版本。无论如何,我很确定问题出在以下项目中:
- 了解
null = null
不返回TRUE
而是返回null
null
值在执行left join
后出现在b.userid
和a.incidentstag
中>
关于android - SQLite Left Outer Join 在 Android 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122220/