有一表tb
  PAR_NO    SON_NO
  A          A1
  A          A2
  A          A3
  A          A11
  A1         A12
  A11        A111
  A11        A112
  A3         A112
  A3         A31 
  AA         AA1
  AA         AA2
  AA1        A111
如果有一SON_NO的字段值如'A111',想查询出它的PAR_NO:'A','AA',请问这段程序如何写?

解决方案 »

  1.   

    with TADOQuery.create(nil) do
    begin
      connection := yourconnection;
      sql.text := 'select par_no from tb where son_no='''A111'''';
      open;
      for i:= 1 to recordcount do
        strPAR_NO:=field[0].asstring+',';
      free;
    end;
      

  2.   

    sql.text := 'select par_no from tb where son_no=''A111'''
      

  3.   

    strPAR_NO:='';
    with TADOQuery.create(nil) do
    begin
      connection := yourconnection;
      sql.clear;
      sql.add( 'select par_no from tb where son_no=:sno');
      parameters[0].value:='a111';
      open;
      first;
      while not eof() do begin 
        strPAR_NO:=strPAR_NO+','+field[0].asstring;
        next;
      end;
      strPAR_NO:=copy(strPAR_NO,2);
      free;
    end;
      

  4.   

    select PAR_NO from tb
    where PAR_NO not in (select SON_NO from tb  group by SON_NO)
    and left(PAR_NO,1)=left('A111',1) group by PAR_NO