mysql模糊匹配like及批量替换replace

作者 : 超级管理员 本文共4564个字,预计阅读时间需要12分钟 发布时间: 2021-08-23 共126人阅读

1、mysql 模糊匹配 like 与 not like 用法 :

SELECT * FROM `user` where `nickname` LIKE ‘%测试%’

SELECT * FROM `user` where `nickname` not  LIKE ‘%测试%’

 

2、mysql 批量替换replace函数用法 :

 

  • 替换某个字段,replace可以替换某个字段中的指定的某个部分,replace(column_name,oldregexstr,newreplacestr)
  • 替换表一行,如果原表中有该条数据,那么会将该条数据删除;如果原表中没有该条数据,则会插入指定值

 

mysql> select replace(’12_34_56′,’_’,’&’);
+—————————–+
| replace(’12_34_56′,’_’,’&’) |
+—————————–+
| 12&34&56                    |
+—————————–+

mysql> select * from user;
+—-+————–+—————+——+————-+———-+———+
| ID | user_id_tree | nickname      | NAME | DESCRIPTION | QUANTITY | created |
+—-+————–+—————+——+————-+———-+———+
|  1 | 1_2_3        | 测试_kong_123 | 1111 | NULL        | NULL     | NULL    |
+—-+————–+—————+——+————-+———-+———+
1 row in set

mysql>    UPDATE user set `user_id_tree`= replace( `user_id_tree`, ‘_2_3’, ‘&2&3’)  where `id`=1;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from user;
+—-+————–+—————+——+————-+———-+———+
| ID | user_id_tree | nickname      | NAME | DESCRIPTION | QUANTITY | created |
+—-+————–+—————+——+————-+———-+———+
|  1 | 1&2&3        | 测试_kong_123 | 1111 | NULL        | NULL     | NULL    |
+—-+————–+—————+——+————-+———-+———+
1 row in set

mysql> replace into user (name) value (222);
Query OK, 1 row affected
mysql> select * from user;
+—-+————–+—————+——+————-+———-+———+
| ID | user_id_tree | nickname      | NAME | DESCRIPTION | QUANTITY | created |
+—-+————–+—————+——+————-+———-+———+
|  1 | 1&2&3        | 测试_kong_123 | 1111 | NULL        | NULL     | NULL    |
|  9 | NULL         | NULL          | 222  | NULL        | NULL     | NULL    |
+—-+————–+—————+——+————-+———-+———+
2 rows in set

3、备注:mysql 帮忙命令可以查看详细的命令说明,使用help或问号

比如查看replace函数的使用说明:help replace ? replace

 

root@localhost : xxx_server 10:52:03>? replace;
Name: ‘REPLACE’
Description:
Syntax:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] …)]
[(col_name [, col_name] …)]
{VALUES | VALUE} (value_list) [, (value_list)] …

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] …)]
SET assignment_list

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] …)]
[(col_name [, col_name] …)]
SELECT …

value:
{expr | DEFAULT}

value_list:
value [, value] …

assignment:
col_name = value

assignment_list:
assignment [, assignment] …

REPLACE works exactly like INSERT, except that if an old row in the
table has the same value as a new row for a PRIMARY KEY or a UNIQUE
index, the old row is deleted before the new row is inserted. See [HELP
INSERT].

REPLACE is a MySQL extension to the SQL standard. It either inserts, or
deletes and inserts. For another MySQL extension to standard SQL—that
either inserts or updates—see
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html.

DELAYED inserts and replaces were deprecated in MySQL 5.6. In MySQL
5.7, DELAYED is not supported. The server recognizes but ignores the
DELAYED keyword, handles the replace as a nondelayed replace, and
generates an ER_WARN_LEGACY_SYNTAX_CONVERTED warning. (“REPLACE DELAYED
is no longer supported. The statement was converted to REPLACE.”) The
DELAYED keyword will be removed in a future release.

*Note*:

REPLACE makes sense only if a table has a PRIMARY KEY or UNIQUE index.
Otherwise, it becomes equivalent to INSERT, because there is no index
to be used to determine whether a new row duplicates another.

Values for all columns are taken from the values specified in the
REPLACE statement. Any missing columns are set to their default values,
just as happens for INSERT. You cannot refer to values from the current
row and use them in the new row. If you use an assignment such as SET
col_name = col_name + 1, the reference to the column name on the right
hand side is treated as DEFAULT(col_name), so the assignment is
equivalent to SET col_name = DEFAULT(col_name) + 1.

To use REPLACE, you must have both the INSERT and DELETE privileges for
the table.

If a generated column is replaced explicitly, the only permitted value
is DEFAULT. For information about generated columns, see
http://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.h
tml.

REPLACE supports explicit partition selection using the PARTITION
keyword with a list of comma-separated names of partitions,
subpartitions, or both. As with INSERT, if it is not possible to insert
the new row into any of these partitions or subpartitions, the REPLACE
statement fails with the error Found a row not matching the given
partition set. For more information and examples, see
http://dev.mysql.com/doc/refman/5.7/en/partitioning-selection.html.

URL: http://dev.mysql.com/doc/refman/5.7/en/replace.html

本站所发布的资源均来源于互联网,仅限用于研究学习,不得将软件用于商业或者非法用途,否则一切后果请用户自负!如果侵犯了您的权益请与我们联系!您必须在下载后的24个小时之内,从您的手机和电脑中彻底删除。 如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请与我们联系处理!
互利网 » mysql模糊匹配like及批量替换replace

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整:可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。

发表评论