[英]python + sqlite3: using IS NOT in a join query
This works the way I expect (c is a cursor) 这按我期望的方式工作(c是游标)
c.execute('''SELECT e1.file, e1.sourceUrl, e1.rev
FROM externals1 AS e1
LEFT JOIN externals as e0
ON (e1.file = e0.file)
AND (e1.rev <> e0.rev)
''')
but I get an error here: 但我在这里得到一个错误:
c.execute('''SELECT e1.file, e1.sourceUrl, e1.rev
FROM externals1 AS e1
LEFT JOIN externals as e0
ON (e1.file = e0.file)
AND (e1.rev IS NOT e0.rev)
''')
with the message 与消息
sqlite3.OperationalError: near "e0": syntax error
What's going on? 这是怎么回事? I want to handle the case where e0.rev is NULL.
我想处理e0.rev为NULL的情况。
!=
is ANSI for not equals, what you posted in the second example is invalid SQL for any database I know of. !=
是ANSI(不等于),在第二个示例中发布的内容对于我所知道的任何数据库都是无效的SQL。 You could try: 您可以尝试:
SELECT e1.file, e1.sourceUrl, e1.rev
FROM externals1 AS e1
LEFT JOIN externals as e0 ON e0.file = e1.file
AND e1.rev NOT IN (e0.rev)
SELECT e1.file, e1.sourceUrl, e1.rev
FROM externals1 AS e1
LEFT JOIN externals as e0
ON (e1.file = e0.file)
AND (e1.rev IS NOT NULL)
NULL means 'unassigned or unknown value', and as such can never be equal or not equal to something else. NULL表示“未分配或未知值”,因此永远不能等于或不等于其他值。 (How can something unknown or unassigned be compared to something else unknown or unassigned?) Therefore, you can't use
<>
or =
to test them; (如何将未知或未分配的事物与其他未知或未分配的事物进行比较?)因此,您不能使用
<>
或=
进行测试; you have to use the special operators IS/IS NOT instead. 您必须改为使用特殊运算符IS / IS NOT。
You can also use 您也可以使用
NOT (e1.rev = e0.rev)
if you're wanting to test for non-equality. 如果您要测试非平等。
SELECT e1.file, e1.sourceUrl, e1.rev
FROM externals1 AS e1
LEFT JOIN externals as e0
ON e0.file = e1.file
AND e1.rev <> e0.rev
AND e1.rev IS NOT NULL
The constructs are operand IS NULL
and operand IS NOT NULL
(there is only one operand to either the IS NUll
/ IS NOT NULL
operators !) -- I imagine the NULL-check in addition to a 'not' is desired but it's hard to tell from the question. 构造是
operand IS NULL
和operand IS NOT NULL
( IS NUll
/ IS NOT NULL
运算符只有一个操作数 !)-我想需要除了'not'之外的NULL校验,但是很难从问题中分辨出来。
Remember, x <> y
is always true when either (or both) x
or y
are NULL
. 请记住,当
x
或y
中的一个(或两个)均为NULL
时, x <> y
始终为true。 (At least for an SQL89-compliant engine.) (至少对于兼容SQL89的引擎。)
Happy SQL'ing. 快乐的SQLing。
Addition: The Wikipedia article on NULL is an interesting read, in general. 另外: 关于NULL的Wikipedia文章通常是一个有趣的读物。 It talks about some of the base NULL constructs (which may or may not entirely apply to a particular DB...)
它讨论了一些基本的NULL构造(可能或可能不完全适用于特定的DB ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.