Pesquisa em tabelas diferentes

Delphi

02/05/2003

Olá pessoal
Tenho 3 tabelas no interbase: cao, caovenda e caooutros
Eu sei que pra fazer uma pesquisa eu simplesmente coloco uma query e um datasource pra cada e faço as coisinhas e tal. Mas isso é pra cada um...
Eu queria, ao inves de fazer pesquisa em cao, pesquisa em caovenda e pesquisa em caooutros... fazer uma pesquisa em todos os caes.. q envolveria essas 3 tabelas...
alguém pode dar um help?


Moonlight

Moonlight

Curtidas 0

Respostas

Leandronanet

Leandronanet

02/05/2003

Faça assim no SQL da query Select * From Tabela1, Tabela2


GOSTEI 0
Moonlight

Moonlight

02/05/2003

blz.. mas qdo eu fizer ´and nome= ´´´+ edtNome.Text +´´´ ´ ele vai olhar nas 3 tabelas? tipo, as 3 tem o atributo nome. Ele vai olhar se cao.nome=edtNome.Text e se caovenda.nome=edtNome.Text e se caooutros.nome=edtNome.Text?
pq eu nao tou especificando.. só colocando nome=...
ele vai olhar nas 3?


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

02/05/2003

Olá pessoal Tenho 3 tabelas no interbase: cao, caovenda e caooutros Eu sei que pra fazer uma pesquisa eu simplesmente coloco uma query e um datasource pra cada e faço as coisinhas e tal. Mas isso é pra cada um... Eu queria, ao inves de fazer pesquisa em cao, pesquisa em caovenda e pesquisa em caooutros... fazer uma pesquisa em todos os caes.. q envolveria essas 3 tabelas... alguém pode dar um help?


select * from cao
where nome = :nome
union
select * from caovenda
where nome = :nome
union
select * from caooutros
where nome = :nome


É só passar o parâmetro que a consulta acima vai verificar nas tres tabelas e juntar o conteúdo. Da forma que coloquei, as tres tabelas devem possuir as mesmas definições dados, mas se você não avançar mais, por favor, nos avise.


GOSTEI 0
Moonlight

Moonlight

02/05/2003

blz, tou tentando aqui.
Muito obrigada.
Apanhando um pouco acho que sai.
Mas se eu nao tiver uma coluna em uma das tabelas.
Tipo, eu tenho NOME em cao, caooutros mas nao tenho em caovenda.
aí ele me diz aki q o numero de colunas nao bate com o numero de variaveis na lista.. ou coisa do tipo.
Como faz nesse caso?


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

02/05/2003

Letícia,

Por gentileza, me passa a estrutura dessas tabelas e mais algum detalhes que você julgar necessário. Te mando o script assim que possível.


GOSTEI 0
Moonlight

Moonlight

02/05/2003

Agora deu, mas fiz uma leve gambiarra.
adicionei uma coluna ´nome´ na tabela que nao tinha essa coluna e pronto... afinal de contas, qdo o usuario for inserir, ele nao vai inserir nessa coluna(vai estar sempre vazia) e quando for pesquisar, o que vai acontecer é que não vai achar lá se procurar por nome (o que faz sentido, pq os cães a venda não tem nome.
Era isso msm q vc faria? ou tem solução melhor?
Vou colocar um post abaixo com a estrutura e como montei o sql...


GOSTEI 0
Moonlight

Moonlight

02/05/2003

Tabela CAO:

CODIGO INTEGER NOT NULL NOME VARCHAR(60) NOT NULL RACA VARCHAR(30) MAE VARCHAR(60) PAI VARCHAR(60) SEXO VARCHAR(2) PEDIGREE VARCHAR(2) OBSERVACOES VARCHAR(300) DTNASCIMENTO TIMESTAMP FOTO BLOB SUB_TYPE 0 SEGMENT SIZE 80


Tabela CAOOUTROS
CODIGO INTEGER NOT NULL NOME VARCHAR(60) NOT NULL SEXO VARCHAR(2) PEDIGREE VARCHAR(2) DTNASCIMENTO TIMESTAMP RACA VARCHAR(30) MAE VARCHAR(60) PAI VARCHAR(60) OBSERVACOES VARCHAR(300) PROPRIETARIO VARCHAR(50) TELPROP VARCHAR(10) FOTO BLOB SUB_TYPE 0 SEGMENT SIZE 80


tabela CAOVENDA
CODIGO INTEGER NOT NULL SEXO VARCHAR(2) PEDIGREE VARCHAR(2) DTNASCIMENTO TIMESTAMP RACA VARCHAR(30) MAE VARCHAR(60) PAI VARCHAR(60) PRECO VARCHAR(10) OBSERVACOES VARCHAR(300) FOTO BLOB SUB_TYPE 0 SEGMENT SIZE 80 NOME VARCHAR(60)


como eu fiz pra montar o sql pra pesquisa (acho que ficou redundante, talvez.. mas foi como eu consegui... se tiver jeito melhor, gostaria de aprender :
procedure TfrmPesquisa.sbtPesquisarClick(Sender: TObject); begin if ((chkLoja.Checked=False) and (chkVenda.Checked=False) and (chkOutros.Checked=False)) then begin Application.MessageBox(´Marque pelo menos uma das opções de pesquisa!´,´Alerta´,MB_ICONEXCLAMATION); Abort; end; qryCaesTodos.Close; qryCaesTodos.SQL.Clear; if (chkLoja.Checked=True) then begin qryCaesTodos.SQL.Text:=´Select codigo, nome, raca, pai, mae, pedigree, dtnascimento, sexo´+#10 +´from cao´+10; qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´; if (chkNome.Checked) then begin qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´; end; if (chkPai.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´; end; if (chkMae.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´; end; if (chkSexo.Checked) then begin if (cbSexo.Text=´Fêmeas´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´F´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´M´´; end; if (chkRaca.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ; end; if (chkDtNasc.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´; end; if (chkPedigree.Checked) then begin if (cbPedigree.Text=´Com Pedigree´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´S´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´N´´; end; end; if ((chkVenda.Checked) and (chkLoja.Checked)) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´ union ´; end; if (chkVenda.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text + ´Select codigo, nome,raca, pai, mae, pedigree, dtnascimento, sexo´+#10 +´from caovenda´+10; qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´; if (chkNome.Checked) then begin qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´; end; if (chkPai.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´; end; if (chkMae.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´; end; if (chkSexo.Checked) then begin if (cbSexo.Text=´Fêmeas´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´F´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´M´´; end; if (chkRaca.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ; end; if (chkDtNasc.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´; end; if (chkPedigree.Checked) then begin if (cbPedigree.Text=´Com Pedigree´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´S´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´N´´; end; end; if((chkOutros.Checked) and ( (chkVenda.Checked) or (chkLoja.Checked))) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´ union ´; end; if (chkOutros.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text + ´Select codigo,nome, raca, pai, mae, pedigree, dtnascimento, sexo´+#10 +´from caooutros´+10; qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´; if (chkNome.Checked) then begin qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´; end; if (chkPai.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´; end; if (chkMae.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´; end; if (chkSexo.Checked) then begin if (cbSexo.Text=´Fêmeas´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´F´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= ´M´´; end; if (chkRaca.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ; end; if (chkDtNasc.Checked) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´; end; if (chkPedigree.Checked) then begin if (cbPedigree.Text=´Com Pedigree´) then begin qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´S´ ´; end else qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= ´N´´; end; end; qryCaesTodos.Open; end;



GOSTEI 0
Moonlight

Moonlight

02/05/2003

desculpe...
deixa eu arrumar o codigo aqui pra dar pra visualizar melhor...
procedure TfrmPesquisa.sbtPesquisarClick(Sender: TObject);
begin
   if ((chkLoja.Checked=False) and (chkVenda.Checked=False) and (chkOutros.Checked=False)) then
      begin
      Application.MessageBox(´Marque pelo menos uma das opções de pesquisa!´,´Alerta´,MB_ICONEXCLAMATION);
      Abort;
      end;
   qryCaesTodos.Close;
   qryCaesTodos.SQL.Clear;
   if (chkLoja.Checked=True) then
    begin
     qryCaesTodos.SQL.Text:=´Select codigo, nome, raca, pai, mae, pedigree, dtnascimento, sexo´+#10
     +´from cao´+10;
     qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´;
     if (chkNome.Checked) then
       begin
         qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´;
       end;
    if (chkPai.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´;
      end;
    if (chkMae.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´;
      end;
    if (chkSexo.Checked) then
      begin
        if (cbSexo.Text=´Fêmeas´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "F" ´;
          end
         else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "M"´;
     end;
    if (chkRaca.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ;
      end;
    if (chkDtNasc.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´;
      end;
    if (chkPedigree.Checked) then
      begin
        if (cbPedigree.Text=´Com Pedigree´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "S" ´;
          end
        else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "N"´;
     end;
  end;
  if ((chkVenda.Checked) and (chkLoja.Checked)) then
    begin
      qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´ union ´;
    end;
  if (chkVenda.Checked) then
  begin
     qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text + ´Select codigo, nome,raca, pai, mae, pedigree, dtnascimento, sexo´+#10
     +´from caovenda´+10;
     qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´;
     if (chkNome.Checked) then
       begin
         qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´;
       end;     
    if (chkPai.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´;
      end;
    if (chkMae.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´;
      end;
    if (chkSexo.Checked) then
      begin
        if (cbSexo.Text=´Fêmeas´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "F" ´;
          end
         else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "M"´;
     end;
    if (chkRaca.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ;
      end;
    if (chkDtNasc.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´;
      end;
    if (chkPedigree.Checked) then
      begin
        if (cbPedigree.Text=´Com Pedigree´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "S" ´;
          end
        else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "N"´;
     end;
  end;
  if((chkOutros.Checked) and ( (chkVenda.Checked) or (chkLoja.Checked))) then
    begin
      qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´ union ´;
    end;
  if (chkOutros.Checked) then
    begin
     qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text + ´Select codigo,nome, raca, pai, mae, pedigree, dtnascimento, sexo´+#10
     +´from caooutros´+10;
     qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text+´ where codigo>0´;
     if (chkNome.Checked) then
       begin
         qryCaesTodos.SQL.Text:=qryCaesTodos.SQL.Text + ´and nome= ´´´+ edtNome.Text +´´´ ´;
       end;
    if (chkPai.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pai= ´´´+ edtPai.Text +´´´ ´;
      end;
    if (chkMae.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Mae= ´´´+ edtMae.Text +´´´ ´;
      end;
    if (chkSexo.Checked) then
      begin
        if (cbSexo.Text=´Fêmeas´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "F" ´;
          end
         else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Sexo= "M"´;
     end;
    if (chkRaca.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Raca= ´´´+dbcRaca.Text+´´´ ´ ;
      end;
    if (chkDtNasc.Checked) then
      begin
        qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text+´and DtNascimento= ´´´+ rxdtData.Text +´´´ ´;
      end;
    if (chkPedigree.Checked) then
      begin
        if (cbPedigree.Text=´Com Pedigree´) then
          begin
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "S" ´;
          end
        else
            qryCaesTodos.SQL.Text:= qryCaesTodos.SQL.Text +´and Pedigree= "N"´;
     end;
  end;
  qryCaesTodos.Open;
end;



GOSTEI 0
Aroldo Zanela

Aroldo Zanela

02/05/2003

Dependendo do Servidor de BD dá pra resolver no próprio select. Mas, uma vez resolvido, ok.

Exemplo no select (No caso SQLServer 2000):

.
.
.
union
Select codigo, Space(60) as nome, raca, pai, mae, pedigree, dtnascimento, sexo ...



GOSTEI 0
Moonlight

Moonlight

02/05/2003

estou usando o interbase
bom, está resolvido
mto obrigada, pessoal e principalmente Aroldo
valeu msm


GOSTEI 0
POSTAR