是否可以创建一个表,然后在一个查询中从中进行选择?

时间:2013-05-22 07:16:59

标签: mysql

我很好奇是否有办法在桌子上进行选择,这本身就是选择的产物。

中的某些东西(伪)
    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可以查询该结果。

如果不是这样,我很想知道我可以使用的模糊方法。

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

2 个答案:

答案 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()