我想实现以下功能,在form1中的edit文本框中输入单号,保存时判断这个单号是否存在于另外三个表,
表table1,table2,table3中单号以不同字段表示,比如,dh1,dh2,dh3.
不知道该怎么写了。

解决方案 »

  1.   

    select count(1) as aNum from (
    select dh1 from table1 where dh1=QuotedStr(edit1.text)
    union all
    select dh2 from table2 where dh2=QuotedStr(edit1.text)
    union all
    select dh3 from table3 where dh3=QuotedStr(edit1.text) )  as p如果查询出来的值为3说明三个表中都有,否则没有在三个表中存在。
      

  2.   

    if not exists (select dh1 from table1 where dh1=QuotedStr(edit1.text))
      select -1
    else if not exists (select dh1 from table2 where dh1=QuotedStr(edit1.text))
      select -2
    else if not exists (select dh1 from table3 where dh1=QuotedStr(edit1.text))
      select -3
    else select 0 
      

  3.   

    那就只要统计出来的aNum>=1即可了
      

  4.   

    转换成delphi语言呢,用query组件,太笨了,初学者
      

  5.   

    with adoquery1 do begin
      close;sql.clear;
      sql.add('select count(1) as aNum from ');
      sql.add(' ( select dh1 from table1 where dh1='+QuotedStr(edit1.text) );
      sql.add(' union all ');
      sql.add(' select dh2 from table2 where dh2='+QuotedStr(edit1.text) )'
      sql.add(' union all ')
      sql.add(' select dh3 from table3 where dh3='+QuotedStr(edit1.text)+' )  as p ')
      open;
      if fieldByName('aNum').asinteger>=1 then
         showmessage('该编号存在')
      else
         showmessage('没有找到相关的编号');
    end;
      

  6.   

    两处标点错了:with adoquery1 do begin 
      close;sql.clear; 
      sql.add('select count(1) as aNum from '); 
      sql.add(' ( select dh1 from table1 where dh1='+QuotedStr(edit1.text) ); 
      sql.add(' union all '); 
      sql.add(' select dh2 from table2 where dh2='+QuotedStr(edit1.text) ); 
      sql.add(' union all ') ;  sql.add(' select dh3 from table3 where dh3='+QuotedStr(edit1.text)+' )  as p ') ;  open; 
      if fieldByName('aNum').asinteger>=1 then 
        showmessage('该编号存在') 
      else 
        showmessage('没有找到相关的编号'); 
    end;
      

  7.   

    没刷新,谢谢tgbd了,非常感谢!