【问题标题】:Same Query, Different Results; a Subquery Problem相同的查询,不同的结果;子查询问题
【发布时间】:2021-11-19 08:51:22
【问题描述】:

我是 SQL 新手。我正在使用 SQLite 编码并使用开源 Chinook 数据库。我有两个查询,从我的角度来看,它们应该返回相同的输出;但是,输出是不同的。为什么我的“SumOfUnitPrice”列的结果不同?


Chinook 数据库的 ER 图

查询 1:

select tracks.albumid, sum(tracks.unitprice) as SumOfUnitPrice from tracks                
group by tracks.albumid having tracks.albumid in 
    (select albums.albumid from albums where albums.artistid in 
        (select artists.ArtistId from artists where artists.Name = "Audioslave"
        )
    )

结果 1:

+---------+----------------+                                                                  
| AlbumId | SumOfUnitPrice |                                                                 
+---------+----------------+                                                                  
|      10 |          13.86 |                                                                  
|      11 |          11.88 |                                                                  
|     271 |          13.86 |                                                                 
+---------+----------------+  

查询 2:

select albums.albumid,                                                                  
(select sum(tracks.unitprice)  from tracks                                                
group by tracks.albumid having tracks.albumid in 
    (select albums.albumid from albums where albums.artistid in 
        (select artists.ArtistId from artists where artists.Name = "Audioslave"
        )
    )                                                                                         
) as SumOfUnitPrice                                                                                                                                      
from albums where albums.artistid in                                                    
    (select artists.ArtistId from artists where artists.Name = "Audioslave"
    )

结果 2:

+---------+----------------+                                                                  
| AlbumId | SumOfUnitPrice |                                                                 
+---------+----------------+                                                                  
|      10 |          13.86 |                                                                  
|      11 |          13.86 |                                                                  
|     271 |          13.86 |                                                                 
+---------+----------------+

【问题讨论】:

  • 孩子们,这就是为什么要避免滥用子查询的原因。这可以通过几个内部连接变得超级解释,为什么需要这么多子查询?

标签: sql sqlite subquery correlated-subquery


【解决方案1】:

问题是,您在查询 2 的选择块中的子查询与外部查询无关。 SQLite 会将子查询的结果限制为第一条记录,因此总是返回 13.86。在其他 RDBMS 中,这将导致异常,即您的子查询返回多行。

这应该提供与查询 1 相同的结果:

    select  a.albumid,                                                                  
           ( select sum(t.unitprice)  
             from tracks t
             where t.albumid = a.albumid                                                
             group by t.albumid
           ) as SumOfUnitPrice                                                                                                                                      
    from albums a where a.artistid in                                                    
(select artists.ArtistId from artists where artists.Name = "Audioslave")

更好的解决方案是使用连接:

    select  t.albumid
            sum(t.unitprice) as sum_of_unitprice
    from tracks  t
    join albums a on a.albumid = t.albumid
    join artists ar on ar.artistid = a.artistid 
    where ar.name = "Audioslave"
    group by t.albumid

【讨论】:

    【解决方案2】:

    您的第一个查询在第二个查询中是正确的,由于子查询返回多条记录,您的查询给您带来了错误。 您只需要在下面的第二个查询中添加 where 子句即可获得相同的结果。 无论如何,使用连接查询的最佳实践。由Dominik Klug提供

    select Outer_albums.albumid,                                                                  
        (select sum(tracks.unitprice)  from tracks                                                
        where tracks.albumid = Outer_albums.albumid
             having tracks.albumid in 
            (select albums.albumid from albums where albums.artistid in 
                (select artists.ArtistId from artists where artists.Name = "Audioslave"
                )
            )                                                                                         
        ) as SumOfUnitPrice                                                                                                                                      
        from albums Outer_albums
         where Outer_albums.artistid in                                                    
            (select artists.ArtistId from artists where artists.Name = "Audioslave"
            )
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多