sql查询中%%%的含义?

sql查询中%%%的含义?,sql,Sql,我熟悉这种查询: select * from tableA where foo like '%bar%' 但今天我在一些遗留代码中遇到了三个相邻的百分号,如下所示: select * from tableA where foo like '%%%' 当foo是字符串类型(varchar等)时,这个查询似乎在mssql和oracle上都能工作,但当foo是数字时它会失败 知道这是什么意思吗 编辑:对于原始问题中的输入错误,很抱歉,查询使用LIKE运算符。您是否希望它能够处理数字类型,尤其是当您

我熟悉这种查询:

select * from tableA where foo like '%bar%'
但今天我在一些遗留代码中遇到了三个相邻的百分号,如下所示:

select * from tableA where foo like '%%%'
当foo是字符串类型(varchar等)时,这个查询似乎在mssql和oracle上都能工作,但当foo是数字时它会失败

知道这是什么意思吗


编辑:对于原始问题中的输入错误,很抱歉,查询使用LIKE运算符。

您是否希望它能够处理数字类型,尤其是当您传入字符串以比较字段时

另外,foo字段在其中存储了什么

在进行通配符比较时,通常使用
LIKE
子句。但是,从您的示例来看,db中似乎有一个字段存储通配符条件。而且,%%%似乎是指任何只包含%%%作为其值的东西(特别是当您将其与=号进行比较时)


EDIT2:如果我猜对了,
比如“%%”
可能表示任何包含%字符的内容

如果要查找任何包含百分比的值,则需要使用ESCAPE:

e、 g

如果foo是数值的(在数据类型中),那么如果您试图将列中的数值与字符串进行比较,则会出现错误

select * from tableA where foo='%%%'
相当于

select * from tableA where foo='%'
这和我的想法一样

ls *

这意味着它可以匹配任何东西。 在这个例子中: 对于MySQL,“%”匹配所有内容 对于ls,“*”匹配所有内容

如果你加2%的话,它等于1

mysql> create table foo ( bar char(20) );
Query OK, 0 rows affected (0.06 sec)

mysql> desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| bar   | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into foo values ('endwith%');
Query OK, 1 row affected (0.05 sec)

mysql> insert into foo values ('%startwith');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('cont%ins');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('doesnotcontain');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo where bar like '%%%';
+----------------+
| bar            |
+----------------+
| endwith%       |
| %startwith     |
| cont%ins       |
| doesnotcontain |
+----------------+
4 rows in set (0.00 sec)
在SQL中,“%”和“\”(下划线符号)是通配符。
'%'
替换任意数量的字符-相当于Linux/Unix和Windows文件搜索中的
'*'

替换Linux/Unix和Windows文件搜索中的一个字符-相当于
?”


正如前面提到的,一个“%”相当于任意数量的连续“%”,因此在您的查询中,您可以将
'%%'
替换为
'%'

在mysql中,它匹配所有内容:

mysql> create database foo;
Query OK, 1 row affected (0.06 sec)

mysql> use foo;
Database changed

mysql> create table foo ( bar char(20) );
Query OK, 0 rows affected (0.06 sec)

mysql> desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| bar   | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into foo values ('endwith%');
Query OK, 1 row affected (0.05 sec)

mysql> insert into foo values ('%startwith');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('cont%ins');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('doesnotcontain');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo where bar like '%%%';
+----------------+
| bar            |
+----------------+
| endwith%       |
| %startwith     |
| cont%ins       |
| doesnotcontain |
+----------------+
4 rows in set (0.00 sec)

当我们使用诸如like之类的运算符比较字符串时,我们会在引号中写入字符串:
从emp中选择*,其中ename类似“国王”

对于数值比较,我们不将值放在引号中,而是使用逻辑运算符,如=:
从emp中选择*,其中deptno=20

它不是只搜索字符串
%%
?还是我遗漏了什么?这不像
。你是说“where foo like%%%”而不是像Kobi说的“where foo='%%%”吗。当您使用的是
=
而不是类似于
的操作符时,您的第一个查询将查找
'%bar%'
。%%%并不表示任何包含%字符的内容。要搜索任何包含百分比的值,您需要对其进行转义,例如,WHERE foo-LIKE“%|%%”escape“|”@AdaTheDev:我已经更正了我的答案。OP最初没有提到
LIKE
子句。不,您使用Edit2再次使其不正确!与“%”类似,它并不意味着任何在中包含%字符的内容……它只会返回所有记录,而不管它们是否包含%登录……这就是我的原意@阿达德夫:这就是我最初的想法。但是,我不想怀疑原始查询编写者是否可以编写这样一个子句(如果它没有意义的话)。查询编写器可以编写
SELECT*FROM myTable
而不是
SELECT*FROM myTable,其中foo类似于“%%”
。现在请阅读我的答案和我的评论,以便理解我为什么这么说。另外,
ESCAPE
子句是否适用于sqlserver和oracle?@shahkalpesh-是的,它在sqlserver中受支持谢谢。我不知道sql server中的
ESCAPE
子句。如果知道
ls*
ls**
做什么;-)这个答案很有用最好也明确地提到它的作用。
mysql> create table foo ( bar char(20) );
Query OK, 0 rows affected (0.06 sec)

mysql> desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| bar   | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into foo values ('endwith%');
Query OK, 1 row affected (0.05 sec)

mysql> insert into foo values ('%startwith');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('cont%ins');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('doesnotcontain');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo where bar like '%%%';
+----------------+
| bar            |
+----------------+
| endwith%       |
| %startwith     |
| cont%ins       |
| doesnotcontain |
+----------------+
4 rows in set (0.00 sec)
mysql> create database foo;
Query OK, 1 row affected (0.06 sec)

mysql> use foo;
Database changed

mysql> create table foo ( bar char(20) );
Query OK, 0 rows affected (0.06 sec)

mysql> desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| bar   | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into foo values ('endwith%');
Query OK, 1 row affected (0.05 sec)

mysql> insert into foo values ('%startwith');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('cont%ins');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values ('doesnotcontain');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo where bar like '%%%';
+----------------+
| bar            |
+----------------+
| endwith%       |
| %startwith     |
| cont%ins       |
| doesnotcontain |
+----------------+
4 rows in set (0.00 sec)