zoukankan      html  css  js  c++  java
  • SQL相关子查询的例子

    select * from ra_provide order by productid,providerid

    --ra_provide表
    002    0001    150.00
    001    0002    185.00
    002    0002    100.00
    003    0002    145.00
    002    0005    230.00
    001    0010    370.00
    001    0014    87.00
    002    0014    90.00
    002    0016    300.00
    001    0025    200.00
    003    0025    213.00
    002    0028    210.00
    003    0028    185.00
    003    0029    0.00

    select * from jc_provider

    001    厂商1    zyd
    002    厂商2    zyd
    003    厂商3   

    --查询所有供应商都能提供的商品
    select distinct productid from ra_provide a
    where not exists
    (
        select providerid from jc_provider b
        where not exists
            (select * from ra_provide c where a.productid=c.productid and b.providerid=c.providerid
            )
    )

    --选出各ID中的h_id最大的一个!
    Create Table TEST(ID    Int Identity(1,1), h_id    Int)
    Insert TEST Select 100 Union All Select 100 Union All Select 100
    Union All Select 107
    Union All Select 101
    Union All Select 109
    Union All Select 108
    select * from test order by h_id
    --GO--方法一:
    Select * From TEST A Where Id In(Select TOP 3 ID From TEST Where h_id=A.h_id) order by h_id
    --方法二:
    Select * From TEST A Where Not Exists (Select 1 From TEST Where h_id=A.h_id And ID<A.ID Having Count(*)>2)
    --方法三:Select * From TEST A Where (Select Count(*) From TEST Where h_id=A.h_id And ID<A.ID)<3GO
    --方法四
    Select * From @t BInner Join (Select Max(Rq) as Rq,HH From @t Group By hh) AOn A.Rq=B.Rq--用双主键标识啊!Drop Table TESTGO/*ID    h_id1    1002    1003    1004    1015    1016    101*/
    -------------------------------------

    create table xx
    (name char(10),
    class char(10),
    chengji float,
    primary key (name,class)
    )
    insert into xx
    select 'zhang','wuli',90.5 Union All
    select 'li','shuxue',60 Union All
    select 'li','zhengzhi',70
    select * from xx

    select name,class,chengji from xx
    select name,class,chengji from xx a where
    chengji in
    (select max(chengji) cj from xx where name=a.name)

    select * from xx
    select * from xx a
    where exists
    (select avg(chengji) from xx        --76.36
    having avg(chengji)<=a.chengji        --针对聚合函数的条件一定要用having
    )

  • 相关阅读:
    CF 1182F Maximum Sine——根号算法
    左偏树学习笔记
    CF1182 D Complete Mirror——思路
    AT3576 E Popping Balls——计数思路
    loj 6053 简单的函数 —— min_25筛
    bzoj 2784 时间流逝 —— 树上高斯消元
    loj 2542 随机游走 —— 最值反演+树上期望DP+fmt
    poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
    loj 6485 LJJ学二项式定理 —— 单位根反演
    bzoj 3328 PYXFIB —— 单位根反演
  • 原文地址:https://www.cnblogs.com/edong/p/1939262.html
Copyright © 2011-2022 走看看