文档章节

学习 SQL 语句 - Select(3): 条件查询与模糊查询

涂孟超
 涂孟超
发布于 2014/09/26 15:37
字数 1139
阅读 5
收藏 0

Where 用来指定查询条件;

Like 和 Not Like 来指定模糊条件;

模糊条件中:
_ 表示任一字符;
% 表示任一字符串;
[] 表示一个集合.

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB;

type
  TForm1 = 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;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//country 表中 Area<200000 的记录
procedure TForm1.Button1Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Area<200000';
    Open;
  end;
end;

//country 表中 Continent="South America" 的记录; 字符串值应该在引号中(单引号、双引号均可).
procedure TForm1.Button2Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Continent="South America"';
    Open;
  end;
end;

//country 表中 Name="Cuba" 或者 Name="Peru" 的记录
procedure TForm1.Button3Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name="Cuba" or Name="Peru"';
    Open;
  end;
end;

//country 表中 Continent="South America" 并且 Area>1000000 的记录
procedure TForm1.Button4Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Continent="South America" and Area>1000000';
    Open;
  end;
end;

//country 表中 Name 是 c 开头的记录; 其中的 % 表示任意字符串
procedure TForm1.Button5Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "c%"';
    Open;
  end;
end;

//country 表中 Name 是 b 或 c 开头的记录; 可以用 "," 隔开更多条件
procedure TForm1.Button6Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "[b,c]%"';
    Open;
  end;
end;

//country 表中 Name 是 a 或 b 或 c 开头的记录
procedure TForm1.Button7Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "[a-c]%"';
    Open;
  end;
end;

//country 表中 Name 不是 a 或 b 或 c 或 m 开头的记录
procedure TForm1.Button8Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name NOT LIKE "[a-c,m]%"';
    Open;
  end;
end;

//country 表中 Name 不是 a 或 b 或 c 开头的记录
procedure TForm1.Button9Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name NOT LIKE "[a-c]%"';
    Open;
  end;
end;

//country 表中 Name 包含 er 的记录
procedure TForm1.Button10Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "%er%"';
    Open;
  end;
end;

//country 表中 Name 包含空格的记录
procedure TForm1.Button11Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "% %"';
    Open;
  end;
end;

//country 表中 Name 第二个字符任意, 但第一字符是 p、第三字符是 r 的记录; "_" 表示任意字符
procedure TForm1.Button12Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "p_r%"';
    Open;
  end;
end;

//country 表中 Name 是 4 个字符的记录
procedure TForm1.Button13Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "____"';
    Open;
  end;
end;

//country 表中 Name 是 4 个字符, 但最后是 a 结尾的记录
procedure TForm1.Button14Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT * FROM country WHERE Name LIKE "___a"';
    Open;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  mdbFile: string;
begin
  mdbFile := 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: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 407
  ClientWidth = 626
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 0
    Top = 65
    Width = 626
    Height = 342
    Align = alClient
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 626
    Height = 65
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 1
    object Button1: TButton
      Left = 6
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button1'
      TabOrder = 0
      OnClick = Button1Click
    end
    object Button2: TButton
      Left = 87
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button2'
      TabOrder = 1
      OnClick = Button2Click
    end
    object Button3: TButton
      Left = 168
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button3'
      TabOrder = 2
      OnClick = Button3Click
    end
    object Button4: TButton
      Left = 249
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button4'
      TabOrder = 3
      OnClick = Button4Click
    end
    object Button5: TButton
      Left = 330
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button5'
      TabOrder = 4
      OnClick = Button5Click
    end
    object Button6: TButton
      Left = 411
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button6'
      TabOrder = 5
      OnClick = Button6Click
    end
    object Button7: TButton
      Left = 492
      Top = 5
      Width = 75
      Height = 25
      Caption = 'Button7'
      TabOrder = 6
      OnClick = Button7Click
    end
    object Button8: TButton
      Left = 6
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button8'
      TabOrder = 7
      OnClick = Button8Click
    end
    object Button9: TButton
      Left = 87
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button9'
      TabOrder = 8
      OnClick = Button9Click
    end
    object Button10: TButton
      Left = 168
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button10'
      TabOrder = 9
      OnClick = Button10Click
    end
    object Button11: TButton
      Left = 249
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button11'
      TabOrder = 10
      OnClick = Button11Click
    end
    object Button12: TButton
      Left = 330
      Top = 34
      Width = 75
      Height = 25
      Caption = 'Button12'
      TabOrder = 11
      OnClick = Button12Click
    end
    object Button13: TButton
      Left = 411
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button13'
      TabOrder = 12
      OnClick = Button13Click
    end
    object Button14: TButton
      Left = 492
      Top = 36
      Width = 75
      Height = 25
      Caption = 'Button14'
      TabOrder = 13
      OnClick = Button14Click
    end
  end
  object DataSource1: TDataSource
    DataSet = ADODataSet1
    Left = 184
    Top = 112
  end
  object ADODataSet1: TADODataSet
    CursorType = ctStatic
    Parameters = <>
    Left = 232
    Top = 184
  end
end

 
 
 
 
 

 

 

  

本文转载自:http://www.cnblogs.com/del/archive/2009/05/28/1491287.html

共有 人打赏支持
涂孟超
粉丝 12
博文 2011
码字总数 14107
作品 0
深圳
程序员
哪些SQL语句会引起全表扫描

本文导读:大家都知道,用SQL语句对数据库进行操作时,如果引起全表扫描会对数据库的性能形成影响,下面向大家简单介绍SQL中哪些情况会引起全表扫描。 1、模糊查询效率很低: 原因:like本身...

zray4u
2016/10/18
81
0
在使用ibatis实现多条件模糊查询的语句

大家一般能想到的做法是,将String语句进行拼串例如: String sql1="select * from 表名where"; String sql2="条件1"; String sql3="条件2"; String sql4="条件3"; ...... String sqln="条件n...

xinlll
2012/10/23
0
0
SQL模糊查询详解

在进行数据库查询时,有两种:完整查询和模糊查询,模糊查询语句如下: SELECT 字段 from 表 WHERE 某字段 Like 条件 其中关于查询的条件,SQL提供了四种匹配模式:%、 _、 []、 [^] 1 % 表示...

长平狐
2013/01/06
47
0
iOS-SQLite3和FMDB使用

1 SQLite3 1> SQLite的语法 管理工具:Navicat 数据类型 { text 字段 integer 数字 real 浮点 bolb 二进制 可以完整保存数据,例如照片 } ddl(结构定义 CREATE ALTER DROP) { CREATE CREATE...

Ethan-GOGO
2015/10/11
1K
0
MySQL单表多字段模糊查询解决方法

在最近的一个项目需要实现在单表中对多字段进行多个关键字的模糊查询,但这数个关键字并不一定都存在于某个字段 例如现有table表,其中有title,tag,description三个字段,分别记录一条资料...

BearCatYN
2014/09/20
0
1

没有更多内容

加载失败,请刷新页面

加载更多

下一页

C++ gflags

###定义参数 gflags主要支持的参数类型包括bool,int32, int64, uint64, double, string等,定义参数通过DEFINE_type宏实现, 该宏的三个参数含义分别为命令行参数名,参数默认值,以及参数的...

SibylY
15分钟前
0
0
intellij IDEA Properties中文unicode转码问题

在IDEA中创建了properties文件,发现默认中文不会自动进行unicode转码。如下 在project settings - File Encoding,在标红的选项上打上勾,确定即可 效果图如下: unicode转码后效果...

muzi1994
16分钟前
0
0
Java IO类库之PipedWriter

一、PipedWriter介绍 PipedWriter是字符管道输出流,继承自Writer,功能与PipedOutputStream类似,通过与PipedReader组合使用实现类似管道的功能,在多线程环境下,一个线程使用PipedWriter...

老韭菜
20分钟前
0
0
精简分页组件(手写)

需要引入CSS(没错就是这4行) .pagelist { text-align: center; color: #666; width: 100%; clear: both; margin: 20px 0; padding-top: 20px }.pagelist a { color: #666; margin: 0 2px;......

AK灬
21分钟前
3
0
29 岁成为阿里巴巴 P8,工作前 5 年完成晋升 3 连跳,他如何做到?

泡泡是我的好朋友。今年 31 岁,毕业后就进了阿里巴巴,工作五年内从 P4 晋升至 P6、P7、P8。 和他很少聊到工作,但总觉得他有很棒的职场心得,应该分享出来,于是有了这次采访。希望对职场新...

Java填坑之路
23分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部