Table of Contents
在SQL结构化查询语言中,LIKE语句有着至关重要的作用。 
Home Database Mysql Tutorial sql语句中like匹配的用法详解_MySQL

sql语句中like匹配的用法详解_MySQL

Jun 01, 2016 pm 01:26 PM
sql statement where

bitsCN.com

在SQL结构化查询语言中,LIKE语句有着至关重要的作用。 

LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用是在一个字符型字段列中检索包含对应子串的。 
假设有一个数据库中有个表table1,在table1中有两个字段,分别是name和sex二者全是字符型数据。现在我们要在姓名字段中查询以“张”字开头的记录,语句如下: 
Java代码 
  1. select * from table1 where name like "张*"  
如果要查询以“张”结尾的记录,则语句如下: 
Java代码 
  1. select * from table1 where name like "*张"  
这里用到了通配符“*”,可以说,like语句是和通配符分不开的。下面我们就详细介绍一下通配符。 
匹配类型   
模式 
举例 及 代表值 
说明 

多个字符 

c*c代表cc,cBc,cbc,cabdfec等 
它同于DOS命令中的通配符,代表多个字符。 

多个字符 

%c%代表agdcagd等 
这种方法在很多程序中要用到,主要是查询包含子串的。 

特殊字符 
  • a
  • a代表a*a
  • 代替* 

    单字符 

    b?b代表brb,bFb等 
    同于DOS命令中的?通配符,代表单个字符 

    单数字 

    k#k代表k1k,k8k,k0k 
    大致同上,不同的是代只能代表单个数字。 

    字符范围 
    - [a-z]代表a到z的26个字母中任意一个 指定一个范围中任意一个 
    续上 
    排除 [!字符] [!a-z]代表9,0,%,*等 它只代表单个字符 
    数字排除 [!数字] [!0-9]代表A,b,C,d等 同上 
    组合类型 字符[范围类型]字符 cc[!a-d]#代表ccF#等 可以和其它几种方式组合使用 
    假设表table1中有以下记录: 
        name                          sex 
                    张小明              男 
        李明天       男 
        李a天       女 
        王5五       男 
        王清五           男 

    下面我们来举例说明一下: 
    例1,查询name字段中包含有“明”字的。 
    select * from table1 where name like '%明%'  

    例2,查询name字段中以“李”字开头。 
    select * from table1 where name like '李*'  

    例3,查询name字段中含有数字的。 
     select * from table1 where name like '%[0-9]%'  

    例4,查询name字段中含有小写字母的。 
    select * from table1 where name like '%[a-z]%'  

    例5,查询name字段中不含有数字的。 
    select * from table1 where name like '%[!0-9]%'  

    以上例子能列出什么值来显而易见。但在这里,我们着重要说明的是通配符“*”与“%”的区别。 
    很多朋友会问,为什么我在以上查询时有个别的表示所有字符的时候用"%"而不用“*”? 
    例子结果: 
    1. select * from table1 where name like *明*  
    2.   select * from table1 where name like %明%  

    大家会看到,前一条语句列出来的是所有的记录,而后一条记录列出来的是name字段中含有“明”的记录,所以说,当我们作字符型字段包含一个子串的查询时最好采用“%”而不用“*”,用“*”的时候只在开头或者只在结尾时,而不能两端全由“*”代替任意字符的情况下。 
    更多有关mysql数据库的内容,请参考:http://www.jbxue.com/db/mysql。bitsCN.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to use the iif function in excel How to use the iif function in excel Mar 20, 2024 pm 06:10 PM

    Most users use Excel to process table data. In fact, Excel also has a VBA program. Apart from experts, not many users have used this function. The iif function is often used when writing in VBA. It is actually the same as if The functions of the functions are similar. Let me introduce to you the usage of the iif function. There are iif functions in SQL statements and VBA code in Excel. The iif function is similar to the IF function in the excel worksheet. It performs true and false value judgment and returns different results based on the logically calculated true and false values. IF function usage is (condition, yes, no). IF statement and IIF function in VBA. The former IF statement is a control statement that can execute different statements according to conditions. The latter

    How to query oracle database logs How to query oracle database logs Apr 07, 2024 pm 04:51 PM

    Oracle database log information can be queried by the following methods: Use SQL statements to query from the v$log view; use the LogMiner tool to analyze log files; use the ALTER SYSTEM command to view the status of the current log file; use the TRACE command to view information about specific events; use operations System tools look at the end of the log file.

    How to use sql statement to query the storage structure of mysql database How to use sql statement to query the storage structure of mysql database Apr 14, 2024 pm 07:45 PM

    To query the MySQL database storage structure, you can use the following SQL statement: SHOW CREATE TABLE table_name; this statement will return the column definition and table option information of the table, including column name, data type, constraints and general properties of the table, such as storage engine and character set.

    How to export the queried data in navicat How to export the queried data in navicat Apr 24, 2024 am 04:15 AM

    Export query results in Navicat: Execute query. Right-click the query results and select Export Data. Select the export format as needed: CSV: Field separator is comma. Excel: Includes table headers, using Excel format. SQL script: Contains SQL statements used to recreate query results. Select export options (such as encoding, line breaks). Select the export location and file name. Click "Export" to start the export.

    How to solve mysql database initialization failure How to solve mysql database initialization failure Apr 14, 2024 pm 07:12 PM

    To resolve the MySQL database initialization failure issue, follow these steps: Check permissions and make sure you are using a user with appropriate permissions. If the database already exists, delete it or choose a different name. If the table already exists, delete it or choose a different name. Check the SQL statement for syntax errors. Confirm that the MySQL server is running and connectable. Verify that you are using the correct port number. Check the MySQL log file or Error Code Finder for details of other errors.

    How to execute sql statement in mysql database How to execute sql statement in mysql database Apr 14, 2024 pm 07:48 PM

    MySQL SQL statements can be executed by: Using the MySQL CLI (Command Line Interface): Log in to the database and enter the SQL statement. Using MySQL Workbench: Start the application, connect to the database, and execute statements. Use a programming language: import the MySQL connection library, create a database connection, and execute statements. Use other tools such as DB Browser for SQLite: download and install the application, open the database file, and execute the statements.

    MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

    MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

    Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

    MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages ​​respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

    See all articles