学习 sql 语句 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
学习 sql 语句 - select(3): 条件查询与模糊查询
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
where 用来指定查询条件;
like 和 not like 来指定模糊条件;
模糊条件中:
_ 表示任一字符;
% 表示任一字符串;
[] 表示一个集合.
本例效果图:
代码文件:
unit unit1;interfaceuseswindows, messages, sysutils, variants, classes, graphics, controls, forms,dialogs, stdctrls, extctrls, grids, dbgrids, db, adodb;typetform1 = class(tform)dbgrid1: tdbgrid;datasource1: tdatasource;adodataset1: tadodataset;panel1: tpanel;button1: tbutton;button2: tbutton;button3: tbutton;button4: tbutton;button5: tbutton;button6: tbutton;button7: tbutton;button8: tbutton;button9: tbutton;button10: tbutton;button11: tbutton;button12: tbutton;button13: tbutton;button14: tbutton;procedure formcreate(sender: tobject);procedure button1click(sender: tobject);procedure button2click(sender: tobject);procedure button3click(sender: tobject);procedure button4click(sender: tobject);procedure button5click(sender: tobject);procedure button6click(sender: tobject);procedure button7click(sender: tobject);procedure button8click(sender: tobject);procedure button9click(sender: tobject);procedure button10click(sender: tobject);procedure button11click(sender: tobject);procedure button12click(sender: tobject);procedure button13click(sender: tobject);procedure button14click(sender: tobject);end;varform1: tform1;implementation{$r *.dfm}//country 表中 area<200000 的记录 procedure tform1.button1click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where area<200000';open;end; end;//country 表中 continent="south america" 的记录; 字符串值应该在引号中(单引号、双引号均可). procedure tform1.button2click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where continent="south america"';open;end; end;//country 表中 name="cuba" 或者 name="peru" 的记录 procedure tform1.button3click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name="cuba" or name="peru"';open;end; end;//country 表中 continent="south america" 并且 area>1000000 的记录 procedure tform1.button4click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where continent="south america" and area>1000000';open;end; end;//country 表中 name 是 c 开头的记录; 其中的 % 表示任意字符串 procedure tform1.button5click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "c%"';open;end; end;//country 表中 name 是 b 或 c 开头的记录; 可以用 "," 隔开更多条件 procedure tform1.button6click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "[b,c]%"';open;end; end;//country 表中 name 是 a 或 b 或 c 开头的记录 procedure tform1.button7click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "[a-c]%"';open;end; end;//country 表中 name 不是 a 或 b 或 c 或 m 开头的记录 procedure tform1.button8click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name not like "[a-c,m]%"';open;end; end;//country 表中 name 不是 a 或 b 或 c 开头的记录 procedure tform1.button9click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name not like "[a-c]%"';open;end; end;//country 表中 name 包含 er 的记录 procedure tform1.button10click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "%er%"';open;end; end;//country 表中 name 包含空格的记录 procedure tform1.button11click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "% %"';open;end; end;//country 表中 name 第二个字符任意, 但第一字符是 p、第三字符是 r 的记录; "_" 表示任意字符 procedure tform1.button12click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "p_r%"';open;end; end;//country 表中 name 是 4 个字符的记录 procedure tform1.button13click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "____"';open;end; end;//country 表中 name 是 4 个字符, 但最后是 a 结尾的记录 procedure tform1.button14click(sender: tobject); beginwith adodataset1 do beginclose;commandtext := 'select * from country where name like "___a"';open;end; end;procedure tform1.formcreate(sender: tobject); varmdbfile: string; beginmdbfile := getenvironmentvariable('commonprogramfiles');mdbfile := mdbfile '\codegear shared\data\dbdemos.mdb';adodataset1.connectionstring := 'provider=microsoft.jet.oledb.4.0;data source=' mdbfile ';persist security info=false';dbgrid1.datasource := datasource1;datasource1.dataset := adodataset1; end;end.
窗体文件:
object form1: tform1left = 0top = 0caption = 'form1'clientheight = 407clientwidth = 626color = clbtnfacefont.charset = default_charsetfont.color = clwindowtextfont.height = -11font.name = 'tahoma'font.style = []oldcreateorder = falseoncreate = formcreatepixelsperinch = 96textheight = 13object dbgrid1: tdbgridleft = 0top = 65width = 626height = 342align = alclientdatasource = datasource1taborder = 0titlefont.charset = default_charsettitlefont.color = clwindowtexttitlefont.height = -11titlefont.name = 'tahoma'titlefont.style = []endobject panel1: tpanelleft = 0top = 0width = 626height = 65align = altopcaption = 'panel1'taborder = 1object button1: tbuttonleft = 6top = 5width = 75height = 25caption = 'button1'taborder = 0onclick = button1clickendobject button2: tbuttonleft = 87top = 5width = 75height = 25caption = 'button2'taborder = 1onclick = button2clickendobject button3: tbuttonleft = 168top = 5width = 75height = 25caption = 'button3'taborder = 2onclick = button3clickendobject button4: tbuttonleft = 249top = 5width = 75height = 25caption = 'button4'taborder = 3onclick = button4clickendobject button5: tbuttonleft = 330top = 5width = 75height = 25caption = 'button5'taborder = 4onclick = button5clickendobject button6: tbuttonleft = 411top = 5width = 75height = 25caption = 'button6'taborder = 5onclick = button6clickendobject button7: tbuttonleft = 492top = 5width = 75height = 25caption = 'button7'taborder = 6onclick = button7clickendobject button8: tbuttonleft = 6top = 36width = 75height = 25caption = 'button8'taborder = 7onclick = button8clickendobject button9: tbuttonleft = 87top = 36width = 75height = 25caption = 'button9'taborder = 8onclick = button9clickendobject button10: tbuttonleft = 168top = 36width = 75height = 25caption = 'button10'taborder = 9onclick = button10clickendobject button11: tbuttonleft = 249top = 36width = 75height = 25caption = 'button11'taborder = 10onclick = button11clickendobject button12: tbuttonleft = 330top = 34width = 75height = 25caption = 'button12'taborder = 11onclick = button12clickendobject button13: tbuttonleft = 411top = 36width = 75height = 25caption = 'button13'taborder = 12onclick = button13clickendobject button14: tbuttonleft = 492top = 36width = 75height = 25caption = 'button14'taborder = 13onclick = button14clickendendobject datasource1: tdatasourcedataset = adodataset1left = 184top = 112endobject adodataset1: tadodatasetcursortype = ctstaticparameters = <>left = 232top = 184end end
转载于:https://www.cnblogs.com/del/archive/2009/05/28/1491287.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的学习 sql 语句 - select(3): 条件查询与模糊查询的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: