【问题标题】:Nested query in MySqlMySql 中的嵌套查询
【发布时间】:2013-10-11 13:37:20
【问题描述】:

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

CustomerName, City, Revenue

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

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

此查询基于另一个查询。可以在 MYSQL 中做同样的事情吗?

谢谢

【问题讨论】:

    标签: mysql sql ms-access


    【解决方案1】:

    是的,像这样

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

    你必须给子查询起别名。

    【讨论】:

      【解决方案2】:

      您可以创建VIEW

      VIEW 就像一个表格,但实际上是一个或多个表格的选择。

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          您可以使用子查询来做到这一点。

          select count(*) as Tot
          
          from ( 
            select City from some_table
          ) c
          
          where c.City = 'YOURCITY'
          

          c 在这种情况下,是子查询的别名。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-15
            • 2019-05-05
            • 2012-02-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多