線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:740
推到 Plurk!
推到 Facebook!

欄位搜尋的優先順序?

答題得分者是:Fishman
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-07-10 23:54:47 IP:61.71.xxx.xxx 未訂閱
請問各位!    如下的記錄    第一筆 A欄位 123  B欄位 789 第二筆 A欄位 456  B欄位 123    我利用一個CHECKBOX 來選擇優先順序是A欄位或B欄位, 如下HAPI     
if CheckBox1.Checked then 
   sql='select * from table1 where A= :iNO or B= :iNO'
else
   sql='select * from table1 where B= :iNO or A= :iNO';
我要查詢123的值(可能在A或B), 下 Table1.ParambyName('iNO').Value:= '123'; 如果選擇A欄位優先(即CheckBox1.Checked=true), 可以找到第一筆記錄, 但如 果我想先找B欄位時, 利用上述的語法(即CheckBox1.Checked=false)仍然是找到 第一筆而不是我想找出的第二筆記錄, 顯然我的想法是不正確的, 那要如何才行 透過SQL來解決A,B欄位的優先尋找順序 PS:我使用FireBird1.5(InterBase)的sql 謝謝! 發表人 - P.D. 於 2005/07/10 23:57:16
Fishman
尊榮會員


發表:120
回覆:1949
積分:2163
註冊:2006-10-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-07-11 08:58:08 IP:210.65.xxx.xxx 未訂閱
Hi P.D. 大大,    不知這樣是否是你要的?
procedure TForm1.Button1Click(Sender: TObject);
var
  sql : String;
begin
  Query1.Close;
  Query1.SQL.Clear;
  Query1.SQL.Add('select * from table1 where (A= :iNO and :Check = '   QuotedStr('Y')   ') or (B= :iNO and :Check = '   QuotedStr('N')   ')');
  if CheckBox1.Checked then
    Query1.ParamByName('check').AsString = 'Y'
  else
    Query1.ParamByName('check').AsString = 'N';
  Query1.ParambyName('iNO').Value:= '123';
  Query1.Open;
end;
---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-07-11 09:04:20 IP:210.61.xxx.xxx 未訂閱
P.D.您好    用order by或union可以達到,
declare @temp table(a integer,b integer)    declare @CheckBox char(1)    insert @temp (a,b) values (123,789)
insert @temp (a,b) values (456,123)    set @CheckBox='1'    declare @iNo  integer
set  @iNo=123    if @CheckBox='1' 
begin
   Select * from @temp where a=@iNo or b=@iNo
--   union
 --  select * from @temp where b=@iNo
   order  by  a
end
else
begin
   Select * from @temp where b=@iNo or a=@iNo
--   union
--   select * from @temp where a=@iNo
   order  by  b
end
..................... .楛耕傷稼,楛耘失歲. .....................
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-07-11 11:41:54 IP:61.71.xxx.xxx 未訂閱
感謝兩位的指教, Fishman的作法我不太明白, 而 pedro的做法屬mssql的宣告, 在ib中不方便使用, 但卻給我一個想法, 結果可以達到我要的, 只是不知道是否觀念上正確 if CheckBox1.Checked then sql='select * from table1 where (A= :iNO or B= :iNO) and ' 'A= (select A from table1 where A= :iNO)' else sql='select * from table1 where (B= :iNO or A= :iNO) and ' 'B= (select B from table1 where B= :iNO)'; 這樣就OK了 謝謝!
系統時間:2024-07-05 2:50:04
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!