Fórum Pesquisa em tabelas diferentes #157396
02/05/2003
0
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
Curtir tópico
+ 0Posts
02/05/2003
Leandronanet
Gostei + 0
02/05/2003
Moonlight
pq eu nao tou especificando.. só colocando nome=...
ele vai olhar nas 3?
Gostei + 0
02/05/2003
Aroldo Zanela
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
02/05/2003
Moonlight
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
02/05/2003
Aroldo Zanela
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
02/05/2003
Moonlight
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
02/05/2003
Moonlight
Tabela CAOOUTROS
tabela CAOVENDA
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 :
Gostei + 0
02/05/2003
Moonlight
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
02/05/2003
Aroldo Zanela
Exemplo no select (No caso SQLServer 2000):
. . . union Select codigo, Space(60) as nome, raca, pai, mae, pedigree, dtnascimento, sexo ...
Gostei + 0
02/05/2003
Moonlight
bom, está resolvido
mto obrigada, pessoal e principalmente Aroldo
valeu msm
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)