- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python中的mysql数据库LIKE操作符详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式.
语法:
1
2
3
|
SELECT
column_name(s)
FROM
table_name
WHERE
column_name
LIKE
pattern
|
pattern这里就是放指定模板的地方,而这里就要用到“ % ”,也叫做通配符 。
%如果是放在条件前面,那就是查以...结尾的数据;例如:%李 。
%如果是放在条件后面,那就是查以...开头的数据;例如:李% 。
%如果是在条件前后都存在,那就是查包含的数据;例如:%李% 。
小知识点:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%z' at line 1 。
1064的错误就是LIKE查询时(语法错误),通配符处没加引号,所以才会报错... 。
正确展示例如:"%李%" 。
示例1:终端运行sql且WHERE子句中使用LIKE 。
查询地址以Hang开头的人员信息 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
root@7c6316b19d80:/# mysql -u root -p
Enter
password
:
Welcome
to
the MySQL monitor. Commands
end
with
;
or
\g.
Your MySQL
connection
id
is
140
Server version: 5.6.51 MySQL Community Server (GPL)
mysql> mysql>
select
*
from
test_user
where
address
like
'Hang%'
;
+
----+--------+-------------+----------+
| id |
name
| mobile | address |
+
----+--------+-------------+----------+
| 3 | python | 18856565858 | Hangzhou |
| 4 | java | 17756565858 | Hangzhou |
| 5 | php | 15556565858 | Hangzhou |
| 6 | c# | 17748484142 | Hangzhou |
+
----+--------+-------------+----------+
4
rows
in
set
(0.00 sec)
mysql>
|
查询地址以u结尾的人员信息 。
1
2
3
4
5
6
7
8
9
10
11
|
mysql>
select
*
from
test_user
where
address
like
'%u'
;
+
----+--------+-------------+----------+
| id |
name
| mobile | address |
+
----+--------+-------------+----------+
| 3 | python | 18856565858 | Hangzhou |
| 4 | java | 17756565858 | Hangzhou |
| 5 | php | 15556565858 | Hangzhou |
| 6 | c# | 17748484142 | Hangzhou |
+
----+--------+-------------+----------+
4
rows
in
set
(0.00 sec)
mysql>
|
示例2:使用python脚本执行含LIKE的sql语句 。
查询地址包含z字符的人员信息 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import
pymysql
# 连接数据库
connection
=
pymysql.connect(host
=
"localhost"
, user
=
"root"
, password
=
"123456"
,
database
=
"testing"
, port
=
3306
, charset
=
'utf8'
,
cursorclass
=
pymysql.cursors.DictCursor)
try
:
with connection:
with connection.cursor() as cursor:
sql
=
"""
SELECT
*
FROM
test_user
WHERE
address LIKE '%z%';
"""
cursor.execute(sql)
result
=
cursor.fetchall()
for
i
in
result:
print
(i)
except
pymysql.err.MySQLError as _error:
raise
_error
|
1
2
3
4
5
6
|
{
'id'
:
3
,
'name'
:
'python'
,
'mobile'
:
'18856565858'
,
'address'
:
'Hangzhou'
}
{
'id'
:
4
,
'name'
:
'java'
,
'mobile'
:
'17756565858'
,
'address'
:
'Hangzhou'
}
{
'id'
:
5
,
'name'
:
'php'
,
'mobile'
:
'15556565858'
,
'address'
:
'Hangzhou'
}
{
'id'
:
6
,
'name'
:
'c#'
,
'mobile'
:
'17748484142'
,
'address'
:
'Hangzhou'
}
Process finished with exit code
0
|
查询地址不包含z字符的人员信息 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
try
:
with connection:
with connection.cursor() as cursor:
sql
=
"""
SELECT
*
FROM
test_user
WHERE
address NOT LIKE '%z%';
"""
cursor.execute(sql)
result
=
cursor.fetchall()
for
i
in
result:
print
(i)
except
pymysql.err.MySQLError as _error:
raise
_error
|
1
2
3
4
|
{
'id'
:
1
,
'name'
:
'张三三'
,
'mobile'
:
'17748484141'
,
'address'
:
'浙江杭州'
}
{
'id'
:
9
,
'name'
:
'111'
,
'mobile'
:
'18847474549'
,
'address'
:
'浙江杭州'
}
Process finished with exit code
0
|
至此,使用LIKE操作符查询完毕... 。
知识点扩展:python中的mysql数据库like模糊查询 。
%在python中是个特殊的符号,如%s,%d分别代表了字符串占位符和数字占位符.
大家知道,mysql的模糊查询也需要用到%.
所以,可以先把需要查的字符串抽出来,再以参数方式传入.
1
2
|
args =
'%'
+subtitle+
'%'
sqlQueryTitle=
"select count(*) from tbl_peng_article where title like '%s'"
%args
|
到此这篇关于python中的mysql数据库LIKE操作符详解的文章就介绍到这了,更多相关python mysql like操作符内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/LIFENG0402/article/details/118369315 。
最后此篇关于python中的mysql数据库LIKE操作符详解的文章就讲到这里了,如果你想了解更多关于python中的mysql数据库LIKE操作符详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
算数运算符 a = 1 + 1 // 2 a = 10 - 5 // 5 a = 10 / 5 // 2 a = 10 / 0 // js中除以0不会报错,结果是Infinity a = 2*2
操作符 分类 算术操作符 移位操作符 位操作符 赋值操作符 单目操作符 关系操作符 逻辑操作符 条件操作符 逗号表达式 下标引用、
大家可以与Java中的 == 操作符相互印证一下,加深一下对引用和对象的理解。原问题: Python为什么直接运行和在命令行运行同样语句但结果却不同,他们的缓存机制不同吗? 其实,高票答案已经说得
a: 1---2-3-4--5---6 b: ------T--------- o: ------1234-5---6 使用RxJS,有没有什么算子可以完成上图?我有一个流 A,它是一个随机的事件流,
请查看下面的代码并帮助我理解这个概念equals 方法仅比较对象的值,而 == 运算符比较对象的引用,那么为什么在代码中 == 运算符返回 false,即使创建了两个单独的对象 Ex Obj1
我在love2d引擎上做了一个无关紧要的lua控制台。我正在尝试升级元表以实现更多功能(配对功能,更改另一个表上的元表而不是目标表等),我正在制作的插件之一是 __changeindex修改现有索引时
今天分享一个比较少见的用法,那就是 Python 是有 ,= 这种用法的,至于是它是否称得上操作符,这个不重要。 咱们先一起回顾一下逗号相关的用法吧: 1,元组的组成部分 没有逗号的括号,
按照要求进行的排序: 复制代码 代码如下: #!/bin/perl @number=qw/5 10 15 3 2 4 8 6 /; my @d
请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。 SQL UNION 语法 下面的 SQL
运算符“!”有什么区别?和“-z”应用于字符串? #Example 1 if [ ! STRING ]; then ... #Example 2 if [ -z STRING ]; then ...
我想定义一个形式为 x /==> y 的新运算符, 在哪里 运营商/==>被视为例如/@ Map 的运营商, 和 翻译成 MyFunction[x, y] .有一个重要的方面:我 希望结果运算符在前端
我有一个Bitset类,用于存储chars的vector,并且我希望每当使用cout bit = std::vector ((X + 7)/ 8); 上市: / *构造函数* / friend st
我事先确实在这里找到了一个确切的问题,但我没有相同的问题原因(或者至少我认为是这样)。我的 AnimatedSprite类有 Timer成员,使其不可复制(明确删除了复制 ctor 和 operato
这个问题已经有答案了: What does C++ struct syntax "a : b" mean (5 个回答) ":" (colon) in C struct - what does it
我想过滤我的 json,我尝试使用通配符“%”模拟简单的 mysql 运算符“like”,但使用一个……某种“技巧”。 我们有一些元素: var items = [ {id: 1,name:
我正在使用 Flink v.1.4.0。 我希望能够在 Flink UI 中命名运算符。我知道要这样做,我只需要在 DataSet 或 DataStream 上使用 .name() 方法。例如,像这样
在我的任务中,我被要求创建 Product 类,我已经完成了除“非成员 IO 运算符”之外的所有实现。我发现这个问题非常模糊,它要求我重载 >运算符与 ostream 和 istream 一起从控制台
我想要一个 QTableWidget带有定制的某些电池 QProgressBar s,我希望可以对包含这些的列进行排序。 我的定制QProgressBar继承自 QProgressBar和 QTabl
这在 Ruby 中如何实现?不重复变量就可以做到吗?Javascript: b = a || 7 如果 a 不是 0 则分配 a 否则分配 7 一个具体情况是将 date.wday 转换为 7,如果它
在使用 withLatestFrom 时,我正在努力将参数传递给选择器,这是之前从加载操作有效负载映射的 loadLocalSubServices$: Observable = this.action
我是一名优秀的程序员,十分优秀!