文档章节

学习 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
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
SQL模糊查询详解

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

长平狐
2013/01/06
60
0
MySQL单表多字段模糊查询解决方法

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

BearCatYN
2014/09/20
0
1

没有更多内容

加载失败,请刷新页面

加载更多

CockroachDB

百度云上的CockroachDB 云数据库 帮助文档 > 产品文档 > CockroachDB 云数据库 > 产品描述 开源NewSQL – CockroachDB在百度内部的应用与实践 嘉宾演讲视频及PPT回顾:http://suo.im/5bnORh ...

miaojiangmin
24分钟前
0
0
I2C EEPROM驱动实例分析

上篇分析了Linux Kernel中的I2C驱动框架,本篇举一个具体的I2C设备驱动(eeprom)来对I2C设备驱动有个实际的认识。 s3c24xx系列集成了一个基于I2C的eeprom设备at24cxx系列。at24cxx系列芯片包...

yepanl
26分钟前
1
0
spring mvc拦截器,实现统计http请求的后台运行时间

使用两种方式,实现拦截http请求的后台运行时间。 废话不多说直接上代码 /** * Http请求时间统计 * 拦截所有请求 */public class HttpRquestTimeInterceptor extends HandlerIntercepto...

兜兜毛毛
43分钟前
2
0
设计模式之工厂模式

本篇博文主要翻译这篇文章: https://www.journaldev.com/1392/factory-design-pattern-in-java 由于翻译水平有限,自认为许多地方翻译不恰当,欢迎各位给出宝贵的建议,建议大家去阅读原文。...

firepation
59分钟前
4
0

中国龙-扬科
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部