Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial sql 通配符小问题 在线急等

sql 通配符小问题 在线急等

Jun 23, 2016 pm 01:59 PM

本人mysql菜鸟一枚
遇到通配符的问题求助各位大神

我想选取users表中姓名包含"szo"或者"szö"的数据
所以写了以下代码:

select * from users where name like '%sz[oö]%'
Copy after login

数据库有这样的数据
但是返回值是0
尝试
select * from users where name like '%szo%' or name like '%szö%'
Copy after login

有结果
请问这两个有什么不一样?


回复讨论(解决方案)

mysql like 的通配符只有 "_" 和 "%"
"_"是匹配任意单个字符
"%"是匹配任意多个字符。

select * from users where name like '%sz[oö]%'
的意思就是 name 中包含 sz[oö] 的记录。
而不是 包含 szo 或 szö的记录

like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP

like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果
有别的办法吗 ?

mysql like 的通配符只有 "_" 和 "%"
"_"是匹配任意单个字符
"%"是匹配任意多个字符。

select * from users where name like '%sz[oö]%'
的意思就是 name 中包含 sz[oö] 的记录。
而不是 包含 szo 或 szö的记录
明白了,有解决办法吗

select * from users where name like '%szo%' or name like '%szö%'
你不是已经用这个办法解决了吗?


like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果
有别的办法吗 ?

不会吧?你的regexp是怎么写的?
select * from users where name regexp 'sz[oö]'

SELECT * FROM  usersWHERE  nameREGEXP  '(a-z)*sz(o|ö)(a-z)*'LIMIT 0 , 30
Copy after login
Copy after login
Copy after login

解决了谢谢大家
结帖



like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果
有别的办法吗 ?

不会吧?你的regexp是怎么写的?
select * from users where name regexp 'sz[oö]'
因为 like 不识别 [oö],regexp 不识别 %, 所以拉

SELECT * FROM  usersWHERE  nameREGEXP  '(a-z)*sz(o|ö)(a-z)*'LIMIT 0 , 30
Copy after login
Copy after login
Copy after login

select * from users where name like '%szo%' or name like '%szoö%'



like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果
有别的办法吗 ?

不会吧?你的regexp是怎么写的?
select * from users where name regexp 'sz[oö]'
尝试了一下你的代码 比我的简洁好用
谢谢 不嫌弃我这只菜鸟耐心解答




like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果
有别的办法吗 ?

不会吧?你的regexp是怎么写的?
select * from users where name regexp 'sz[oö]'
因为 like 不识别 [oö],regexp 不识别 %, 所以拉

SELECT * FROM  usersWHERE  nameREGEXP  '(a-z)*sz(o|ö)(a-z)*'LIMIT 0 , 30
Copy after login
Copy after login
Copy after login


既然用了regexp,你还要%有何用? 这就好比是穿着登山鞋去打篮球?

select * from users where name like '%szo%' or name like '%szö%'
你不是已经用这个办法解决了吗?
这个说来话长  , 还有 a ä i ï u ü 好麻烦 




like并不是正则表达式匹配。而只是通配符%和_等匹配,因此并不支持[xy]这种字符组。
你可以尝试REGEXP
尝试过,一样的结果 
有别的办法吗 ?

不会吧?你的regexp是怎么写的?
select * from users where name regexp  'sz[oö]' 
尝试了一下你的代码  比我的简洁好用 
谢谢 不嫌弃我这只菜鸟耐心解答 

客气了。谁不是从菜鸟过来的。
掌握一定的技巧,往往能让学习效率加倍。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles