Dúvida com select
04/02/2006
0
Tenho uma tabela2 com os campos composição1 (string) com apenas 1 palavra, composição2 (string) com apenas 1 palavra e o campo reação(string).
Agora a pesquisa: a pessoa digita num edit1 o nome1, e no edit2 o nome2, ao clicar um botão, deverá fazer o seguinte:
1) buscar o nome1 na tabela1, pegar todos as composições contidas no campo composição e montar uma matriz X de M elementos (de acordo com a quantidade de componentes contidos neste campo);
2) buscar o nome2 na tabela1, pegar todos as composições contidas no campo composição e montar uma matriz Y de N elementos (de acordo com a quantidade de componentes contidos neste campo);
3) montar uma combinação entre as duas matrizes, digamos uma matriz Z para jogar o resultado da pesquisa neste matriz;
4) pegar a matriz X[I] e buscar na tabela2 no campo composição1, se achar então pega a matriz Y[I] e busca na tabela2 no campo composição2, caso ache pega o resultado contido na tabela2, campo reação e joga na matriz Z[I].
Eu não tenho problema em criar as matrizes, nem em criar a combinação, o meu problema está em construir esta SQL
Edilcimar
Posts
04/02/2006
Marco Salles
2)ja que voce esta trabalhando com matriz , como saber quantos elementos tem esta matriz... Tip um recorcCound
04/02/2006
Marco Salles
X[J] := Trim(Temp);
para isto voce deve fazer antes
[b:3cccd45148]setlength(x, 100), [/b:3cccd45148]
mas eu to querendo fazer
Como usa ::
04/02/2006
Edilcimar
Temp := ´´;
J := 1;
SetLength(X,1);
SetLength(Y,1);
With Table1 do
Begin
If Locate(´PRODUTO´,Edit1.Text,[]) then
Texto := Trim(FieldByName(´COMPOSICAO´).AsString)
Else
Begin
ShowMessage(´PRODUTO NÃO ENCONTRADO´);
Abort;
End;
End;
For I := 1 to Length(Texto) do
Begin
If Texto[I] <> ´,´ then
Begin
Temp := Temp + Texto[I];
If I = Length(Texto) then
Begin
X[J-1] := Trim(Temp);
SetLength(X, J);
End;
End
Else
Begin
X[J-1] := Trim(Temp);
J := J + 1;
SetLength(X, J);
Temp := ´´;
End;
End;
With Table1 do
Begin
If Locate(´PRODUTO´,Edit2.Text,[]) then
Texto := Trim(FieldByName(´COMPOSICAO´).AsString)
Else
Begin
ShowMessage(´PRODUTO NÃO ENCONTRADO´);
Abort;
End;
End;
J := 1;
For I := 1 to Length(Texto) do
Begin
If Texto[I] <> ´,´ then
Begin
Temp := Temp + Texto[I];
If I = Length(Texto) then
Begin
Y[J-1] := Trim(Temp);
SetLength(Y, J);
End;
End
Else
Begin
Y[J-1] := Trim(Temp);
J := J + 1;
SetLength(Y, J);
Temp := ´´;
End;
End;
TamanhoX := Length(X);
TamanhoY := Length(Y);
se usar lenght(x) terá o tamanho da matriz, se usar high(x) terá o valor do maior índice da matriz, ou seja length - 1
04/02/2006
Marco Salles
como definir a Matriz Z uma matriz n,2 .. onde o n não se sabe
04/02/2006
Edilcimar
X, Y : array of string;
StLength(X,1);
SetLength(Y,1);
SetLength(X, J);
SetLength(Y J);
04/02/2006
Marco Salles
04/02/2006
Edilcimar
TamanhoX := Length(X);
TamanhoY := Length(Y);
SetLength(Z, TamanhoX * TamanhoY);
04/02/2006
Edilcimar
procedure TForm4.Button2Click(Sender: TObject);
var
I, J, K, TamanhoX, TamanhoY : Integer;
Texto, Temp : String;
X, Y, Z : array of string;
begin
Temp := ´´;
J := 1;
SetLength(X,1);
SetLength(Y,1);
With Table1 do
Begin
If Locate(´PRODUTO´,Edit1.Text,[]) then
Texto := Trim(FieldByName(´COMPOSICAO´).AsString)
Else
Begin
ShowMessage(´PRODUTO NÃO ENCONTRADO´);
Abort;
End;
End;
For I := 1 to Length(Texto) do
Begin
If Texto[I] <> ´,´ then
Begin
Temp := Temp + Texto[I];
If I = Length(Texto) then
Begin
X[J-1] := Trim(Temp);
SetLength(X, J);
End;
End
Else
Begin
X[J-1] := Trim(Temp);
J := J + 1;
SetLength(X, J);
Temp := ´´;
End;
End;
Temp := ´´;
With Table1 do
Begin
If Locate(´PRODUTO´,Edit2.Text,[]) then
Texto := Trim(FieldByName(´COMPOSICAO´).AsString)
Else
Begin
ShowMessage(´PRODUTO NÃO ENCONTRADO´);
Abort;
End;
End;
J := 1;
For I := 1 to Length(Texto) do
Begin
If Texto[I] <> ´,´ then
Begin
Temp := Temp + Texto[I];
If I = Length(Texto) then
Begin
Y[J-1] := Trim(Temp);
SetLength(Y, J);
End;
End
Else
Begin
Y[J-1] := Trim(Temp);
J := J + 1;
SetLength(Y, J);
Temp := ´´;
End;
End;
TamanhoX := Length(X);
TamanhoY := Length(Y);
K := 0;
SetLength(Z, TamanhoX * TamanhoY);
For I := 0 to TamanhoX - 1 do
Begin
For J := 0 to TamanhoY - 1 do
Begin
Z[K] := X[I] + ´+´ + Y[J] +´= ´;
Memo1.Lines.Add(Z[K]);
K := K + 1;
End;
End;
end;
04/02/2006
Marco Salles
falta alguns ajustes ,,,
é apenas o esboço
daqui ha pouco vai
04/02/2006
Edilcimar
04/02/2006
Marco Salles
implementation {$R *.DFM}
procedure CriarMatrizX(Composicao:String); var i,indiceanterior,q:integer; texto:String; begin q:=0; i:=1; indiceAnterior:=1; while i <= length(composicao) do begin if composicao[i]=´;´ then begin texto:=copy(composicao,indiceanterior,i - indiceAnterior); indiceAnterior:=i+1; //aqui voce vaui criar a MatrizX[i] X[q]:=Trim(texto); q:=q+1; form1.Memo1.Lines.Add(texto); FormacaoString:=formacaoString+texto; end; i:=i+1; end; end;
procedure CriarMatrizY(Composicao:String); var i,indiceanterior,q:integer; texto:String; begin setlength(Y, 100); i:=1; indiceAnterior:=1; while i <= length(composicao) do begin if composicao[i]=´;´ then begin texto:=copy(composicao,indiceanterior,i - indiceAnterior); indiceAnterior:=i+1; //aqui voce vai criar a MatrizY[i] Y[q]:=Trim(texto); form1.Memo2.Lines.Add(texto); FormacaoString:=FormacaoString+texto; end; i:=i+1; end; end;
//Veja que é uma reduntancia os dois métodos serem iguais... É so fazer uma passagem de parametro com Var
function GeraCombinacoes(S: string; NumDig: Integer): TStrings; var I, J: Integer; X: Char; TS: TStrings; S1: string; begin Result := TStringList.Create; TS := TStringList.Create; for I := 1 to Length(S) do Result.Add(S[I]); while Length(Result.Strings[0]) < NumDig do begin for I := 0 to (Result.Count - 1) do begin S1 := ´´; for J := 1 to Length(S) do begin X := S[J]; if Pos(X, Result.Strings[I]) = 0 then S1 := S1 + X; end; for J := 1 to Length(S1) do begin X := S1[J]; TS.Add(Result.Strings[I] + X); end; end; Result.Text := TS.Text; TS.Clear; end; TS.Free; end;
//esta função que a partir de X e de Y Gera-la a Matriz Z
//Tb não implementei esta parte.. Coloquei apenas na forma de Memo3
procedure TForm1.Button1Click(Sender: TObject); var i,j,indice:integer; begin //pesquisa na tabela1 Query1.Close; Query1.SQL.Clear; Query1.Sql.Add(´Select *From Tabela1´); Query1.Sql.Add(´Where Produto=:NomePrimeiroProduto´); Query1.ParamByName(´NomePrimeiroProduto´).asstring:=edit1.text; Query1.Sql.Add(´And Produto=:NomesegundoProduto´); Query1.ParamByName(´NomesegundoProduto´).asstring:=edit2.text; Query1.Prepare; Query1.open; Criaçao da MatrizX tamanhox:=0; setlength(x, 100); CriarMatrizX(query1.FieldByName(´Produto´).asstring); Query1.Next; //Criaçao da Matriz Y tamanhoy:=0; setlength(y, 100); CriarMatrizY(query1.FieldByName(´Produto´).asstring); //******ETAPA ABAIXO DEVE SER SUBSTITUIDA Memo3.Lines.Assign(GeraCombinacoes(formacaoString,2)); //aqui voce deve criar a sua matriz Z .. Ela é da forma N,2 //eu não to fazendo este passo..suponha que a partir de Memo3 se consiga isto //Novamente mais uma consulTa , agora na tabela 2 i:=1; for i:=1 to tamanhox do begin for j:=1 to tamanhoy do begin Query2.Close; Query2.SQL.Clear; Query2.Sql.Add(´Select *From Tabela2´); Query2.Sql.Add(´Where (Composicao1=:Comp1´); Query2.Sql.Add(´And Composicao2=:Comp2)´); query2.sql.add(´Or(composicao1:=comp2´); query2.sql.add(´and(composicao2:=comp1))´); Query2.ParamByName(´Comp1´).asstring:=X[i]; Query2.ParamByName(´Comp2´).asstring:=Y[J]; Query2.Prepare; Query2.open; //esta é a lei de formação servira para implementar Z****** Indice:=(i*j)+(i-1)*(3-j); //A formula anteiror no meu conceito e de extrema importancia para //a formação da futura Matriz Z if query2.fieldByName(´Reacao´).AsString <> ´´ Then Memo4.Lines.Add(query2.fieldByName(´Reacao´).AsString) //Z[i,Indice]:=query2.fieldByName(´Reacao´).AsString //nada foi encontrado else Memo4.Lines.Add(´´); // Z[I,indice]:=´´; end; end; end;
[b:cac9145575][b]outro passo importante que não foi mencionado e não foi jevantado e sobre os Produtos da Tabela1.. Por suposição na Tabela1 Nunca Tera 2
Produtos Iguais, isto é os Pordutos são sempre diferentes[/b:cac9145575]
espero que tenha tirado alguma idéia , pois deu trabalho
04/02/2006
Edilcimar
04/02/2006
Edilcimar
Query1.ParamByName(´Comp1´).AsString:=X[I - 1];
Query1.ParamByName(´Comp2´).AsString:=Y[J - 1]; nesta linha, dizendo que query1.parameter ´Comp2´ not found, isto na primeira passada do loop, porém se dou um showmessage no y[j-1] ele me mostra o valor
Clique aqui para fazer login e interagir na Comunidade :)