MySql中的嵌套查询

时间:2013-10-11 13:37:20

标签: mysql sql ms-access

在Ms Ms中,我可以创建一个名为“CustomerList”的查询,其中包含以下数据:

CustomerName, City, Revenue

然后我可以创建另一个查询,例如“CustomerCount”,如:

Select count(*) as Tot
from CustomerList   ( <<<- is a query name)
where CustomerList.City

此查询基于另一个查询。是否有可能在MYSQL中做同样的事情?

由于

4 个答案:

答案 0 :(得分:3)

是的,就像这样

Select count(*) as Tot
from
(
  select City from some_table
) x
where x.City = 'NYC'

您必须为子查询添加别名。

答案 1 :(得分:3)

您可以创建VIEW

VIEW的工作方式类似于表,但实际上是一个或多个表上的选择。

http://dev.mysql.com/doc/refman/5.0/en/create-view.html

答案 2 :(得分:0)

您需要在嵌套查询中使用名为City的列或别名 你只能这样做::

Select count(*) as Tot
from 
/*
select City,..... your query



*/

CustomerList   where CustomerList.City

答案 3 :(得分:0)

您可以使用子查询执行此操作。

select count(*) as Tot

from ( 
  select City from some_table
) c

where c.City = 'YOURCITY'
在这种情况下,

c是子查询的别名。