SQL birden çok alanda arama

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Mustafa973
Üye
Mesajlar: 9
Kayıt: 25 Nis 2006 10:49

SQL birden çok alanda arama

Mesaj gönderen Mustafa973 »

var onay,medeni,cinsiyet,erkek,bayan,bekar,evli,dul,bosanmis,onaylandi,arastirmada,rededildi:String;
begin

erkek:=CheckBox1.Caption;
bayan:= CheckBox2.Caption;
bekar:=CheckBox3.Caption;
evli:=CheckBox4.Caption;
dul:= CheckBox6.Caption;
bosanmis:=CheckBox5.Caption;
onaylandi:=CheckBox7.Caption;
arastirmada:=CheckBox8.Caption;
rededildi:=CheckBox9.Caption;

ADOQuery1.SQL.Clear;

if CheckBox1.Checked=true then cinsiyet:=' and cinsiyet = '''+erkek +'''' ;
if CheckBox2.Checked=true then cinsiyet:=' and cinsiyet = '''+bayan +'''' ;
if CheckBox1.Checked=true and CheckBox2.Checked=true then cinsiyet:='';

if CheckBox3.Checked=true then medeni:=' and medenihali = '''+bekar +'''' ;
if CheckBox4.Checked=true then medeni:=' and medenihali = '''+evli +'''' ;
if CheckBox5.Checked=true then medeni:=' and medenihali = '''+bosanmis +'''' ;
if CheckBox6.Checked=true then medeni:=' and medenihali = '''+dul +'''' ;
if CheckBox3.Checked=true and CheckBox4.Checked=true and CheckBox5.Checked=true and CheckBox6.Checked=true then medeni:='';

if CheckBox7.Checked=true then onay:=' and onaydurumu = '''+onaylandi +'''' ;
if CheckBox8.Checked=true then onay:=' and onaydurumu = '''+arastirmada +'''' ;
if CheckBox9.Checked=true then onay:=' and onaydurumu = '''+rededildi +'''' ;
if CheckBox7.Checked=true and CheckBox8.Checked=true and CheckBox9.Checked=true then onay:='';

ADOQuery1.sql.Add('select * from genelbilgileri where 1=1'+cinsiyet+medeni+onay);
ADOQuery1.Open;
end;



yukarıdaki kod ile birden çok alanla sorgu yapmak istiyorum ama doğru çalışmadı.
onaylanan seçeneklere göre arama yapmıyor onay dışı seçeneklerde listeleniyor. Yardım edecek varmı acaba teşekkürler
mkysoft
Kıdemli Üye
Mesajlar: 3110
Kayıt: 26 Ağu 2003 12:35
Konum: Berlin
İletişim:

Re: SQL birden çok alanda arama

Mesaj gönderen mkysoft »

Kod: Tümünü seç

sql:='select * from genelbilgileri';
and:=' where ';
if CheckBox1.Checked=true then
   sql:=sql+and+'cinsiyet = '''+erkek +'''' ;
   and:= ' and ';
endif
if CheckBox2.Checked=true then
  sql := sql +and+ 'cinsiyet = '''+bayan +'''' ;
   and:= ' and ';
endif
...
....
ADOQuery1.sql.Add(sql);
Bu şekilde düzellerseniz daha kolay olur, ve hata oluşmaz.
Mustafa973
Üye
Mesajlar: 9
Kayıt: 25 Nis 2006 10:49

Re: SQL birden çok alanda arama

Mesaj gönderen Mustafa973 »

Yazdığın kodlarla denedim ama yine istediğim gibi çalışmadı. Aynı anda Erkek ve Bayan onaylayınca sorgu boş çıkıyor. Sorunu çözemedim.
Kullanıcı avatarı
sabanakman
Kıdemli Üye
Mesajlar: 3081
Kayıt: 17 Nis 2006 08:11
Konum: Ah bi Antalya olaydı keşke (Ankara)

Re: SQL birden çok alanda arama

Mesaj gönderen sabanakman »

O zaman and yerine or şartı kullanmalısınız.
Şaban Şahin AKMAN
_________________
Derin olan kuyu değil kısa olan iptir. - .
Cevapla