1

我很好奇是否有办法在表格上进行选择,该表格本身就是选择的产物。类似的东西(伪)

    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可以去查询该结果。

如果不完全是这样,我很想知道我可以使用什么模糊的类似方法。

我需要这个的原因是我有一个相当广泛的数据透视查询,我在我的用户数据上运行,我希望能够从相当大的结果中进行选择。

4

2 回答 2

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

于 2013-05-22T07:28:04.047 回答
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()
于 2013-05-22T07:24:09.227 回答