原表:
Machine Item smallValue bigValue
送板機 長 50 500
送板機 寬 50 400
印刷機 長 50 500
Reflow 長 50 800
Reflow 寬 50 500可見machine中的Item有相同與不同項,並都有他們的最小值(smallValue)與最大值(bigValue)。
1,問長=60的machine?那麼得到:送板機
印刷機
Reflow(因為三種machine的Item-長 都滿足60在smallValue與bigValue之間)2,問長=400和寬=450的machine?那麼得到:印刷機
Reflow因為「送板機」寬不在50-400之間,而「印刷機」沒有寬,「Reflow」符合。求這樣的sql語句?!

解决方案 »

  1.   

    1,問長=60的machine?那麼得到: select * from tb where Item = '长' and (smallValue <= 60 and bigValue >= 60)2,問長=400和寬=450的machine?那麼得到: select * from tb where Item = '长' and (smallValue <= 400 and bigValue >= 400)
    union all
    select * from tb where Item = '宽' and (smallValue <= 450 and bigValue >= 450)
      

  2.   

    1,問長=60的machine?那麼得到: 送板機 
    印刷機 
    Reflow (因為三種machine的Item-長 都滿足60在smallValue與bigValue之間) 
    --
    select machine
    from T
    where Item  = '長' and smallValue <= 60 and bigValue >= 60
      

  3.   


    --一个个答:1,問長=60的machine?那麼得到: 送板機 
    印刷機 
    Reflow select Machine  from 表名 where smallValue<=60 and bigValue>=60 group by  Machine
      

  4.   

    select * 
    from tb 
    where (Item = '长' and (smallValue <= 400 and bigValue >= 400))
          or (Item = '宽' and (smallValue <= 450 and bigValue >= 450))
      

  5.   


    --掉了个“长”的过滤
    1、
    select Machine  from 表名 where Item='长'  smallValue<=60 and bigValue>=60 group by  Machine
      

  6.   

    create table tb(Machine varchar(10) , Item varchar(10) , smallValue int, bigValue int)
    insert into tb values('送板機', '長' ,50, 500) 
    insert into tb values('送板機', '寬' ,50, 400) 
    insert into tb values('印刷機', '長' ,50, 500) 
    insert into tb values('Reflow', '長' ,50, 800) 
    insert into tb values('Reflow', '寬' ,50, 500) 
    go--1,問長=60的machine?那麼得到: 
    select * from tb where Item = '長' and (smallValue <= 60 and bigValue >= 60)
    /*
    Machine    Item       smallValue  bigValue    
    ---------- ---------- ----------- ----------- 
    送板機        長          50          500
    印刷機        長          50          500
    Reflow     長          50          800(所影响的行数为 3 行)
    */--2,問長=400和寬=450的machine?那麼得到: 
    select * from tb where Item = '長' and (smallValue <= 400 and bigValue >= 400)
    union all
    select * from tb where Item = '寬' and (smallValue <= 450 and bigValue >= 450)
    /*
    Machine    Item       smallValue  bigValue    
    ---------- ---------- ----------- ----------- 
    送板機        長          50          500
    印刷機        長          50          500
    Reflow     長          50          800
    Reflow     寬          50          500(所影响的行数为 4 行)
    */
    drop table tb
      

  7.   

    --第二个不知道咋理解,看这两个哪个是你需要的?
    create table tb(Machine varchar(10) , Item varchar(10) , smallValue int, bigValue int)
    insert into tb values('送板機', '長' ,50, 500) 
    insert into tb values('送板機', '寬' ,50, 400) 
    insert into tb values('印刷機', '長' ,50, 500) 
    insert into tb values('Reflow', '長' ,50, 800) 
    insert into tb values('Reflow', '寬' ,50, 500) 
    go--1,問長=60的machine?那麼得到: 
    select * from tb where Item = '長' and (smallValue <= 60 and bigValue >= 60)
    /*
    Machine    Item       smallValue  bigValue    
    ---------- ---------- ----------- ----------- 
    送板機        長          50          500
    印刷機        長          50          500
    Reflow     長          50          800(所影响的行数为 3 行)
    */--2,問長=400和寬=450的machine?那麼得到: 
    select * from tb where Item = '長' and (smallValue <= 400 and bigValue >= 400)
    union all
    select * from tb where Item = '寬' and (smallValue <= 450 and bigValue >= 450)
    /*
    Machine    Item       smallValue  bigValue    
    ---------- ---------- ----------- ----------- 
    送板機        長          50          500
    印刷機        長          50          500
    Reflow     長          50          800
    Reflow     寬          50          500
    (所影响的行数为 4 行)
    */select m.machine from
    (select * from tb where Item = '長' and (smallValue <= 400 and bigValue >= 400)) m,
    (select * from tb where Item = '寬' and (smallValue <= 450 and bigValue >= 450)) n
    where m.machine = n.machine
    /*
    machine    
    ---------- 
    Reflow
    (所影响的行数为 1 行)
    */drop table tb