我有两个表A和B,其中包含以下架构: -
表A - >两列Principal(varchar)和LoginTime(Timestamp)
表B->两列Username(varchar)和LoginTime(Timestamp)
现在我必须在两个表上运行查询,联合它们然后按用户名排序。所以我写了一个这样的查询: -
select Principal AS Uname || ', ' ||
LoginTime from A
where LoginTime > 1430890465
UNION ALL
select Username AS Uname || ', ' ||
LoginTime from B
where LoginTime > 1430890465
order by Uname;
但上述似乎不起作用,并给出以下错误: -
ORA-00923:找不到FROM关键字 00923. 00000 - “未找到FROM关键字” *原因:
*操作:
答案 0 :(得分:3)
校长AS Uname || ',' || LoginTime
语法不正确。您可以连接两列。您不能将列与另一列的别名连接。
由于您要连接username
和timestamp
列,您只需要在连续列中添加别名,即表达式
select (Principal || ', ' ||
LoginTime) AS Uname from A
where LoginTime > 1430890465
UNION ALL
select (Username || ', ' ||
LoginTime) AS Uname from B
where LoginTime > 1430890465
order by Uname;
我已将大括号添加到表达式中,即连接列,以便您轻松了解别名 Uname
是否为连接值
答案 1 :(得分:2)
as
位于要为其提供别名的列之后:
select Principal || ', ' || LoginTime AS Uname
from A
where LoginTime > 1430890465
union all
select Username || ', ' || LoginTime
from B
where LoginTime > 1430890465