Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:使用“的条件之间有什么不同?”;“全部”;及;";在这个查询中?_Sql - Fatal编程技术网

SQL:使用“的条件之间有什么不同?”;“全部”;及;";在这个查询中?

SQL:使用“的条件之间有什么不同?”;“全部”;及;";在这个查询中?,sql,Sql,有什么不同 对: select distinct maker, price from product inner join printer on product.model = printer.model where color = 'y' and price <= (select min(price) from printer where color = 'y') 内克尔 ALL是一个Transact-SQL扩展;并非所有数据库引擎都支持它。哎呀 在谈到数据库时,性能是不容忽视的。mi

有什么不同

对:

select distinct maker, price from product
inner join printer
on product.model = printer.model
where color = 'y' and price <= (select min(price) from printer where color = 'y')
内克尔

  • ALL是一个Transact-SQL扩展;并非所有数据库引擎都支持它。哎呀
  • 在谈到数据库时,性能是不容忽视的。
    min
    解决方案可能会使用索引以极快的速度执行(假设RDBMS构建了一个B树或类似的排序索引),而all解决方案则强制执行完整的表扫描
  • 如果
    price
    允许为空,您将得到不同的答案
    min
    忽略所有空值,因此如果
    price
    为空,则min的结果将是一个数字。但是,您的独特查询的结果将尝试将该空值与所有价格进行比较,这将为每个价格返回
    UNKNOWN
    ,这意味着
    where
    子句中没有价格匹配

  • 如果存在空的
    打印机,则第二个将失败。price
    。考虑这个(我在这里使用PostgreSQL):


    =>select 0 select 0ALL是对所有行展开比较运算符并将条件与and组合的一种简短形式。
    ANY是对所有行展开比较运算符,并将条件与或组合在一起的缩写形式

    具体来说,

    price <= all (select distinct price from printer where color = 'y')
    

    price为什么说第二个查询是错误的?它是否返回错误的结果?或者它不符合某些人的标准?如果有一个索引用于MIN,它就不能将完全相同的索引用于distinct吗?不过,我不同意多重比较。@astander:数据库可以将索引用于
    不同的
    ,但不能将索引用于
    所有的
    。(毕竟,
    all
    意味着对每个结果进行比较)从技术上讲,ANSI规则将对
    NULL
    返回
    UNKNOWN
    (其中
    where
    子句强制为false)+1@BillyONeal:是否有任何数据库具有明确的未知属性?我使用过的所有东西都使用空值。我不认为这两者在功能上有什么区别。MSSQL的文档说,如果启用ANSI NULLS,它将返回一个显式的
    UNKNOWN
    ;但我没有考验自己;因此,
    UNKNOWN
    可能只是文档中表示
    NULL
    .C.J的词。Date对该标准的评论不断浮现在脑海中,意译为:“不要责怪NULL!我也不喜欢它!”将
    NULL
    @Billy进行比较,它在条件的上下文中返回
    FALSE
    ,即它不会继续,这需要一个true。你自己在另一条评论中说它
    强制为FALSE
    ,因此,如果我们仍然在同一参照系(从条件返回)中讨论,那么它可能是FALSE也可能是FALSE;因为where检查true,而null不是true。但是在where中并不会改变条件的行为。(42@RichardTheKiwi:你说得对,我在mssql2008上查过了。
    Product(maker, model, type)
    Printer(code, model, color, type, price)
    
    => select 0 <= (select min(x) from (values (null), (1), (2)) as t(x));
     ?column? 
    ----------
     t
    (1 row)
    
    => select 0 <= all (select x from (values (null), (1), (2)) as t(x));
     ?column? 
    ----------
    
    (1 row)
    
    price <= all (select distinct price from printer where color = 'y')
    
    price <= <price1> AND
    price <= <price2> AND
    price <= <price3> AND
    price <= <price4>
    
    select distinct maker, price from product
    inner join printer
    on product.model = printer.model
    where color = 'y' and price <=
          all (select distinct price from printer where color = 'y'
               where price is not null)