不与子查询一起使用

时间:2019-03-28 03:22:04

标签: sql sql-server subquery

有人要求我在sql服务器数据库中显示以下问题的结果

  

任何HP计算机上未安装哪些软件包?   我尝试了以下操作,但仍能获得PACKNAME Manta的结果,但是该软件包已安装在HP计算机上。我想念什么?

select * from package where PACK in
  ( select PACK from software where TAGNUM in 
   ( select tagnum from PC where comp NOT in
     ( select comp from computer where MFRNAME = 'HP')))

我在下面附上了数据图片供您参考

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以将NOT EXISTS与相关的子查询一起使用,该子查询将其他表连接在一起,并包含您的过滤条件:

select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
                    select 1
                    from software s
                    inner join pc on pc.tagnum = s.tagnum
                    inner join computer c on c.comp = pc.comp
                    where s.pack = pk.pack
                    and c.mfrname = 'HP'
                 )
order by pk.pack;

结果

| pack |         packname | packv |        packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting |   4.1 |      Accounting |   754.95 |
| AC12 |   Accounting MIS |   4.0 |      Accounting |     2000 |
| AC13 |        Quickbook |  2005 |      Accounting |      300 |
| DB11 |            Manta |   1.5 |        Database |      380 |
| DB13 |       SQL Server |  2005 |        Database |      500 |
| DB14 |           My SQL |  2005 |        Database |      300 |
| SS11 |          Easycal |   5.5 |     Spreadsheet |   225.15 |
| WP04 |       Word Power |     2 | Word Processing |      118 |
| WP07 |        Good Word |   3.2 | Word Processing |       35 |
| WP14 |           GOOGLE |     2 | Word Processing |      118 |

SQL Fiddle example

答案 1 :(得分:0)

    self.cl3.refresh_from_db()
    self.assertEqual(
        self.cl3.is_rejected,
        True
    )