我有三个表 aa.bdf  bb.dbf   cc.dbf
aa.dbf中的字段
jh      yc    yx    yxh
name1   ch    0.2   101.1bb.dbf中的字段
jh      jd1     jd2
name1   100.1   200.1cc.dbf中的字段
jh     年月       hs
name1  20061011   0.3我想完成这样的查询:
我想首先为表aa添加一个字段sk,判断aa表中的yxh 字段的值 是否在 bb表中的 jd1 和jd2 之间 如果在sk的值就写 K ,  不在就为空
我已经把 年月这个字段的值都添加到combobox 中
先在combobox中选择有个年月
找aa表中sk  字段的值为K的 和aa表中yx>=0.4 的 和cc表中hs >=30%的如何为aa表添加那个sk字段 如何写最后的那条查询语句

解决方案 »

  1.   

    --增加字段sk
    alter table aa add sk varchar(4) null
    go
    --更新字段sk
    update aa set sk= 'K' from aa a,bb b where a.jb =  b.jh and a.yxh between b.jd1 and b.jd2
    go
    --查询
    select * from aa a,cc b where a.jh =b.jh and a.sk='K' and a.yx>=0.4 and c.hs>=0.3 and c.年月=combobox中的值在DELPHI中有个专门选择日期的控件DateTimePicker,比combobox更好
      

  2.   

    select jh,yc,yx,yhx from (select aa.*, 'sk' =  case when aa.yxh >= bb.jd1 and aa.yxh<=bb.jd2 then 'K', else '' from aa, bb where aa.jh = bb.jh) XXX, cc where yx>=0.4 and sk='k' and hs>=0.3
      

  3.   

    select jh,yc,yx,yhx from (select aa.*, 'sk' = case when aa.yxh >= bb.jd1 and aa.yxh<=bb.jd2 then 'K', else '' end from aa, bb where aa.jh = bb.jh) XXX, cc where yx>=0.4 and sk='k' and hs>=0.3
    修改一下,少了个end