选择多列时,SQL查询返回值,但仅选择一列时,无数据返回

时间:2019-05-12 04:28:16

标签: sql sqlite

仅选择一列时我无法返回数据。此列是一种整数格式,其列名称为“ date”。我正在使用SQLite3。

sqlite> .once table.txt
sqlite> select "date",* from messages limit 3;
sqlite> .once justColumn.txt
sqlite> select "date" from messages limit 3;

输出 table.txt

date|id|folderID|messageKey|conversationID|date|headerMessageID|deleted|jsonAttributes|notability
1556338536000000|32|2|13330|1|1556338536000000|20190427T041536.3792559390306544911.expiry@letsencrypt.org|0|{"43":1,"44":[2],"45":[],"46":[],"50":false,"51":[],"52":[1,2],"53":[2],"55":[[2,1]],"57":[],"58":false,"59":false,"60":false,"61":false}|1
1556339727000000|33|8|2|2|1556339727000000|20190427T043526.4397757710180913572.expiry@letsencrypt.org|0|{"43":1,"44":[2],"45":[],"46":[],"50":false,"51":[],"52":[1,2],"53":[2],"57":[],"58":false,"59":true,"60":false,"61":false}|0
1557332781000000|34|2|13430|3|1557332781000000|1605547663.1557332777559.JavaMail.cfservice@SL132APP2|0|{"43":3,"44":[2],"45":[],"46":[],"50":false,"51":[],"52":[3,2],"53":[2],"55":[[2,3]],"57":[],"58":false,"59":false,"60":false,"61":false}|1

justColumn.txt

date



我希望两个查询在第一列中都返回相同的数据,但是事实并非如此。什么?

1 个答案:

答案 0 :(得分:1)

来自documentation

  

如果返回多行的SELECT语句没有ORDER BY子句,则返回行的顺序不确定。

我可以重现此表中显示的内容:

CREATE TABLE test(id INTEGER PRIMARY KEY, date INTEGER, foo, bar);
INSERT INTO test(date, foo, bar) VALUES (25, 'a', 'b'), (50, 'c', 'd'), (75, 'e', 'f');
INSERT INTO test(foo,bar) VALUES ('g', 'h'), ('i', 'j'), ('k', 'l'); -- null dates
CREATE INDEX test_idx_date ON test(date);

第一个查询:

sqlite> SELECT * FROM test LIMIT 3;
id          date        foo         bar       
----------  ----------  ----------  ----------
1           25          a           b         
2           50          c           d         
3           75          e           f         
sqlite> EXPLAIN QUERY PLAN SELECT * FROM test LIMIT 3;
QUERY PLAN
`--SCAN TABLE test

第二个查询:

sqlite> .null (null)
sqlite> SELECT date FROM test LIMIT 3;
date      
----------
(null)
(null)
(null)
sqlite> EXPLAIN QUERY PLAN SELECT date FROM test LIMIT 3;
QUERY PLAN
`--SCAN TABLE test USING COVERING INDEX test_idx_date

第二个从具有所请求的列而不是整个表的索引中提取结果,并且该索引中的行以与表中不同的顺序返回,因此结果不同。我认为您的餐桌情况也是如此。