欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-02 20:02:34
...

Where 用来指定查询条件;

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

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

本例效果图:

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

代码文件:
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

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/320487