sql查詢語句的拼接小技巧(高手勿噴)

2022-05-15 13:16:03 字數 1167 閱讀 4323

1. 基本的查詢語句後面加上 where 1=1,便於增加查詢條件。

askstr := 'select * from twork where 1=1 ';

if length(cxteworkid.text) <> 0 then

begin

askstr := askstr + ' and gworkid like ' +

quotedstr('%' + cxteworkid.text + '%')

end;

2. 多表查詢可以用乙個字串代替乙個表。邏輯上更清晰。

worktable = 'select gworkid from twork where 1=1 ';

paytable = 'select sum(gxmtotal) as sgxmtotal,gxmtype from tpay where gworkid in ';

paygroupby = ' group by gxmtype ';

askstr := paytable + ' ( ' + worktable + tmpwhere + ' ) ' + paygroupby;

3.自己定義乙個連線函式,判斷資料庫初始化是否成功,便於控制。

function tdm.initdbok: boolean;

begin

result := true;

if fileexists(dbfilename) then

begin

fdconn.drivername := 'sqlite';

fdconn.loginprompt := false;

fdconn.params.clear;

fdconn.params.values['database'] := dbfilename;

fdconn.params.values['driverid'] := 'sqlite';

fdconn.params.values['characterset'] := 'utf8';

tryfdconn.connected := true;

except

result := false;

end;

endelse

result := false;

end;

sql查詢語句的拼接小技巧(高手勿噴)

1.基本的查詢語句後面加上 where 1 1,便於增加查詢條件。askstr select from twork where 1 1 if length cxteworkid.text 0 then begin askstr askstr and gworkid like quotedstr cx...

SQL查詢語句拼接

1.判斷引數是否為空,2.把引數中的空格去掉3.1 1永遠是真,加1 1的目的是為了接後面的條件的,否則 where and 條件 and 條件 是有語法錯誤的string sql select from 表名 where 1 1 if condition1 string.empty 再分享一下我老...

sql更新語句小技巧

日常sql更新語句中,尤其是多表同時更新,或同時更新同乙個表同一條資料,為保證資料準確性!原庫存減少 update lucky user score set now score now score 1 where activity id 9999 and user id 6666 and now s...