表aaa
字段   a      b       c
--------------------------------------
       aaa    a1      c1
       aaa    a2      c2
       aaa    a3      c3
       bbb    1a      1c
       bbb    2a      2c
--------------------------------------
使用两个 ComboBox1, ComboBox2,
首先通过  Sql.Add('Select a,Count(*) from aaa group by a');
字段内容加入ComboBox1中,
ComboBox1内容为:aaa
                 bbb要求当选择ComboBox1为aaa时,
          ComboBox2内容为:a1
                           a2
                           a3
    当选择ComboBox1为bbb时,
          ComboBox2内容为:1a
                           2a如何去做?

解决方案 »

  1.   

    首先第一个,select distinct a from aaa,这样效率高
    第二个,select distinct b from aaa where a:=combobox1.text
    然后把得到的值给combobox2.items
      

  2.   

    在ComboBox1的OnChange事件中:
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select distinct b from aaa where a='''+combobox1.text+'''');
      Open;
      First;
      Combobox2.Items.Clear;
      while not Eof do
      begin
        Combobox2.Items.Add(FieldByName('b').AsString);
        Next;
      end; 
    end;
      

  3.   

    with ADOQuery1 do//先把字段a的值写入combobox1中
    begin
      Close;
      SQL.Clear;
      SQL.Add('select distinct a from aaa );
      Open;
      first;
      Combobox1.Items.Clear;
      while not Eof do
      begin
        Combobox1.Items.Add(FieldByName('a').AsString);
        Next;
      end; 
    end;  在ComboBox1的OnChange事件中:
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select distinct b from aaa where a='''+combobox1.text+'''');
      Open;
      First;
      Combobox2.Items.Clear;
      while not Eof do
      begin
        Combobox2.Items.Add(FieldByName('b').AsString);
        Next;
      end; 
    end;
      

  4.   

    谢谢楼上几位,解决了,不过还要问一下:“distinct“在此作何用?
      

  5.   

    distinct 保证相同值的项只列出一次