zoukankan      html  css  js  c++  java
  • 测试那些事儿—SQL server exists子查询

    exists子查询

    --一次性购买“手机数码”产品的数量超过三个的消费金额打8折

    --根据已知项查询未知项

    --【1】根据类别名称查询类别编号

    select sortid from commoditysort where sortname='手机数码‘

    --【2】根据类别编号查询商品编号

    select commodityid from commodityinfo 

    where sortid=

    (select sortid from commoditysort where sortname='手机数码‘)

    --【3】根据商品编号去查询订单表中的购买数量超过三个的订单信息

    select commodityid from orderinfo

    where commodityid in  --注意此处为in还是=

    (

    select commodityid from commodityinfo 

    where sortid=

    (select sortid from commoditysort where sortname='手机数码‘))

    and amount >3

    --【4】购买超过3个的用户的付款金额打8折

    if exists(

    select commodityid  from orderinfo

    where commodityid in  --注意此处为in还是=

    (

    select commodityid from commodityinfo 

    where sortid=

    (select sortid from commoditysort where sortname='手机数码‘))

    and amount >3

    )

    begin 

     --对付款金额打8折

        update orderinfo set paymoney = paymoney * 0.8

        where commodityid in

        (

    select amount from orderinfo

    where commodityid in  --注意此处为in还是=

    (

    select commodityid from commodityinfo 

    where sortid=

    (select sortid from commoditysort where sortname='手机数码‘))

    and amount >3

    )

    end

    --通常会使用not exists 对子查询结果进行取反

    --exists:子查询查到记录,结果为真,否则结果为假

    --not exists:子查询查不到结果,结果为真。

  • 相关阅读:
    php----爬虫(爬取豆瓣演员信息,搜索页)遇到的问题
    python-写爬虫时遇到的问题 TimeoutError: [WinError 10060]
    聚沙成塔
    买手机,继续纠结中
    问题不绕弯,死磕
    死磕,死磕死磕
    学而不践则罔
    越是忙的时候,兴趣越多
    周末小总结
    幸福和需求
  • 原文地址:https://www.cnblogs.com/mgg520813/p/10935810.html
Copyright © 2011-2022 走看看