SQLite - 比较表达式
(2013-04-22 21:27:54)
标签:
sqlite比较表达式it |
分类: Mac/IOS那些事 |
1.0 比较表达式
1.1 排序
比较操作的结果基于操作数的存储类型,根据下面的规则:
l
2
3
4
1.2 比较操作数的近似(Affinity)
l
2
3
1.3 在比较前的类型转换
l
2 如果一个操作数有TEXT近似,另一个有NONE近似,那么TEXT近似被应用到另一个操作数
3
表达式"a BETWEEN b AND c"表示两个单独的二值比较” a >= b AND a <= c”,即使在两个比较中不同的近似被应用到’a’。
CREATE TABLE t2(
);
INSERT INTO t2 VALUES('500', '500', '500', 500);
SELECT typeof(a), typeof(b), typeof(c), typeof(d) FROM t2;
text|integer|text|integer
-- Because column "a" has text affinity, numeric values on the
-- right-hand +side of the comparisons are converted to text before
-- the comparison occurs.
SELECT a < 40,
0|1|1
-- Text affinity is applied to the right-hand operands but since
-- they are already TEXT this is a no-op; no conversions occur.
SELECT a < '40', a < '60', a < '600' FROM t2;
0|1|1
-- Column "b" has numeric affinity and so numeric affinity is applied
-- to the operands on the right.
-- the application of affinity is a no-op;
no conversions occur.
-- values are compared numerically.
SELECT b < 40,
0|0|1
-- Numeric affinity is applied to operands on the right, converting them
-- from text to integers.
SELECT b < '40', b < '60', b < '600' FROM t2;
0|0|1
-- No affinity conversions
occur.
-- storage class INTEGER which are always less than the TEXT values
-- on the left.
SELECT c < 40,
0|0|0
-- No affinity conversions
occur.
SELECT c < '40', c < '60', c < '600' FROM t2;
0|1|1
-- No affinity conversions
occur.
-- storage class INTEGER which compare numerically with the INTEGER
-- values on the left.
SELECT d < 40,
0|0|1
-- No affinity
conversions occur.
-- always less than TEXT values on the right.
SELECT d < '40', d < '60', d < '600' FROM t2;
1|1|1
从这里可以看出,假如可以使用3.1中的规则进行比较的话,就不需要进行类型转换,否则的话就要进行类型转换
1.4 操作符
所有的数学操作符(+, -, *, /, %, <<, >>, &, |),在被执行前,都会将两个操作数都转换为数值存储类型(INTEGER和REAL)。即使这个转换是有损和不可逆的,转换仍然会执行。一个数学操作符上的NULL操作数将产生NULL结果。一个数学操作符上的操作数,如果以任何方式看都不像数字,并且又不为空的话,将被转换为0或0.src://http://www.cnblogs.com/kfqcome/archive/2011/06/27/2137000.html