支持子查询识别上层查询字段别名
功能描述
Vastbase G100 在MySQL兼容性模式下支持子查询中引用上层查询输出列的别名。
注意事项
该功能仅在数据库兼容模式为MySQL时能够使用(即创建DB时DBCOMPATIBILITY='B'),在其他数据库兼容模式下不能使用该特性。
示例
1、创建兼容模式为MySQL的库db_mysql,并进入。
create database db_mysql dbcompatibility 'B';
\c db_mysql
2、创建测试表。
create table tab_1 (a1 int, b1 int, c1 int, d1 int,e1 text,f1 date);
create table tab_2 (a2 int, b2 int, c2 int, d2 int,e2 text,f2 date);
create table tab_3 (a3 int, b3 int, c3 int, d3 int,e3 text,f3 date);
3、插入测试数据。
insert into tab_1 values
(1, 1, 1, 1,'A10Z','2000-03-01')
,(1, 1, 1, 2,'A1Z' ,'2000-03-02')
,(1, 1, 2, 1,'A11Z','2000-03-05')
,(1, 1, 2, 2,'A2Z' ,'2000-03-04')
,(1, 2, 1, 1,'A13Z','2000-03-05')
,(1, 2, 1, 2,'A3Z' ,'2000-03-19')
,(1, 2, 2, 1,'A15Z','2000-03-05')
,(1, 2, 2, 2,'A4Z' ,'2000-03-19')
,(1, 3, 3, 3,'A10Z','2000-03-05')
,(2, 5, 5, 5,'A5Z' ,'2000-03-10')
,(2, NULL, 6, 6,null,'2000-04-01')
,(2, 6, NULL, 6,'A15Z',null)
,(2, 6, 6, NULL,'A6Z','2000-03-11')
,(2, NULL, NULL, 7,'A5Z','2000-03-02')
,(2, NULL, 7, NULL,'A7Z','2000-03-03')
,(2, 7, NULL, NULL,'A10Z','2000-03-10')
,(3, NULL, NULL, NULL,'A9Z','2000-03-05')
;
insert into tab_1 select generate_series(1, 100)%98, generate_series(1, 100)%20, generate_series(1, 100)%13, generate_series(1, 100)%6,'A'||(generate_series(11,20))||'Z', date'2000-03-01'+generate_series(11,20);
insert into tab_2 select * from tab_1;
insert into tab_2 select generate_series(1, 50)%48, generate_series(1, 50)%28, generate_series(1, 50)%12, generate_series(1, 50)%9,'A'||(generate_series(1,10))||'Z', date'2000-03-01'+generate_series(1,30);
insert into tab_3 select generate_series(1, 100)%98, generate_series(1, 100)%20, generate_series(1, 100)%13, generate_series(1, 100)%6,'A'||(generate_series(1,20))||'Z', date'2000-03-01'+generate_series(1,74);
4、使用上层查询字段别名进行查询。
select a1 alias_a1,b1,c1,d1,e1,f1 from tab_1 where a1 = (with tmp as (select d2 from tab_2 where b2=alias_a1)
select count(*) from (select 'abc', d2 from tmp))
order by 1,2,3,4;
结果返回如下:
alias_a1 | b1 | c1 | d1 | e1 | f1
----------+----+----+----+------+------------
11 | 11 | 11 | 5 | A11Z | 2000-03-12
(1 row)