【问题标题】:How would I convert this query to one using sub queries?如何使用子查询将此查询转换为一个?
【发布时间】:2014-11-03 14:54:31
【问题描述】:

我将如何转换这个块:

select p.ProductNumber, p.Name
from product as p, productsubcategory as ps
where p.ProductSubcategoryID = ps.ProductSubcategoryID
    and ps.Name = 'Pedals'
order by p.ProductNumber;

变成一个只使用子查询而不是使用两个数据库别名的数据库?

【问题讨论】:

  • 别名用于表而不是数据库。

标签: mysql database subquery


【解决方案1】:

以下使用子查询保留表上的别名。表别名是个好主意:

select p.ProductNumber, p.Name
from product p
where p.ProductSubcategoryID in (select ps.ProductSubcategoryID
                                 from productsubcategory ps
                                 where ps.Name = 'Pedals'
                                )
order by p.ProductNumber;

注意:您的原始查询没有问题,只是它应该在 where 子句中使用显式 join 语法而不是隐式 join

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2013-12-19
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多