我很好奇是否有办法在桌子上进行选择,这本身就是选择的产物。
中的某些东西(伪) select a,b,c from
(select id as a, name as b, phone as c, date as d from some_table)
where d = now();
因此,实际上括号将返回一个表,其列由as
定义,然后外部select
可以查询该结果。
如果不是这样,我很想知道我可以使用的模糊方法。
我需要这个的原因是我有一个相当广泛的数据透视查询,我在我的用户数据上运行,我希望能够从相当大的结果中进行选择。
答案 0 :(得分:2)
您可能会收到类似“派生表需要别名”的错误
以下作品:
select a,b,c
from (
select id as a,
name as b,
phone as c,
some_date as d
from some_table
) as t
where d <= now();
别名t
定义了一个所谓的派生表。
SQLFiddle:http://sqlfiddle.com/#!2/05fd6/2
答案 1 :(得分:1)
是的,这是可能的。试试这个:
select A.a,A.b,A.c from
(select id as a, name as b, phone as c, date as d from some_table where d = now()) A
它被称为派生表。
将WHERE
移到子查询之外:
select A.a,A.b,A.c, A.d from
(select id as a, name as b, phone as c, date as d from some_table) A
where A.d = now()