1

测试数据库:

create table a ( d date not null primary key);
create table b ( d date not null primary key);
insert into a values ('2013-01-01');
insert into b values ('2014-01-01');

使用带有 Delphi 7 的 Zeos lib,这些查询都返回 TDateTimeField:

select d from a order by 1;

select d from b order by 1;

select d from a union all select d from b;

select * from (select d from a union all select d from b) s;

然而,这个查询返回一个 TStringField:

select * from (select d from a union all select d from b) s order by 1;

问题:

  • 这是为什么?
  • 我该如何防止这种情况发生?
  • 这是一个错误吗?究竟如何对结果集进行排序会更改列的类型????
  • 这是一个严重的问题,因为我无法从我的程序生成 SQL 查询并在设计时创建的 TZReadOnlyQuery 中打开它们

更新:它也不适用于整数。

create table c ( id integer not null primary key);
create table d ( id integer not null primary key);
insert into c values(1);
insert into d values(2);

这些导致 TLageIntField:

select * from c order by 1

select * from d order by 1

select * from ( select * from c union all select * from d) s

然而,这会产生一个 TStringField:

select * from ( select * from c union all select * from d) s order by 1
4

1 回答 1

1

如此处所述:http ://www.sqlite.org/datatype3.html

SQLite没有为存储日期和/或时间预留存储类。相反,SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值:

TEXT 作为 ISO8601 字符串(“YYYY-MM-DD HH:MM:SS.SSS”)。
REAL 作为儒略日数字,根据预测的公历,自公元前 4714 年 11 月 24 日格林威治中午以来的天数。
INTEGER 作为 Unix 时间,自 1970-01-01 00:00:00 UTC 以来的秒数。应用程序可以选择以任何这些格式存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换。

那么,您可以尝试下面的语句并发布结果吗?!

select date(*) as test from (select date(d) from a union all select date(d) from b) s order by 1;
于 2014-03-27T20:56:55.553 回答