mysql查询操作及正则表达式小结
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 怎么说呢,用markdown编辑好的文本,无法用在博客园中,不知道怎么处理。 一、排序 1、按多个列排序 使用逗号隔开,如果指定方向则紧挨着要排序的列名 对于多个列的排序,先按照前一个排序然后在前一
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
怎么说呢,用markdown编辑好的文本,无法用在博客园中,不知道怎么处理。
一、排序
1、按多个列排序
使用逗号隔开,如果指定方向则紧挨着要排序的列名
对于多个列的排序,先按照前一个排序然后在前一个的基础上按照后面的排序。
如:
SELECT * FROM a2 ORDER BY a_id DESC,t_id desc
数据结果如下:

2、order by与limit
SELECT * FROM a2 ORDER BY a_id DESC,t_id desc LIMIT 1
注意:
order by 的位置,from之后,limit之前。
二、数据过滤操作符(and/or/in/not)
注意:优先级
当含有and和or时,and的优先级高于or,所以先执行and,解决的办法就是使用()括号的优先级高于and,能够消除歧义。
如:
SELECT * FROM a2 WHERE (a_id = 2 or t_id>4) AND id>3
in操作符的特点
in操作符完成了与or相同的功能,如:
SELECT * FROM a2 WHERE t_id in(1,2,3)
SELECT * FROM a2 WHERE t_id =1 OR t_id=2 OR t_id=3
上面两个功能相同。
既然如此,那么为什么还用in呢,下面就说说in的优点:
in的优点:
1.在使用长的合法选项清单时,IN操作符的更清楚直观
2.计算次序容易管理
3.比OR执行快
4.IN最大的优点可以包含其他select语句,能够动态的建立Where子句。
SELECT * FROM a2 WHERE t_id in(
SELECT t_id FROM a2 WHERE id=5
);
NOT 取反
NOT对IN、BETWEEN、exists子句取反。
三、数据过滤通配符
%通配符
%:表示任何字符出现任意次数
LIKE '惠普%' :以'惠普'开头
LIKE '%惠普' :以'惠普'结尾
LIKE '%惠普%':包含'惠普'
LIKE 's%e':以s开头,e结尾
注意:
1.除了一个或多个字符外,%还能匹配0个字符。
2.尾空格可能会干扰通配符匹配,如:保存great时,如果它后面有一个或多个空格,则%great将不会匹配它们,因为后面有多余的字符(空格),解决办法就是使用双%,%great%,一个更好的办法是使用函数去掉首尾空格。
3.%不能匹配NULL.
_通配符
下划线通配符与%相同,但是它只匹配单个字符而非多个。
SELECT * FROM a WHERE name LIKE '_ood' #good
SELECT * FROM a WHERE name LIKE 'goo_' #good
SELECT * FROM a WHERE name LIKE 'go_d' #good
优化:
通配符在搜索处理上比其他的要慢些,所以要记住以下技巧:
1.不要过度使用通配符
2.不要把它们用在搜索模式的开始处,如若不然,搜索起来会很慢的。
3.仔细注意通配符的位置,切勿放错。
四、使用正则表达式
本小节学习如何在Where子句中使用正则表达式来更好的控制数据过滤。
1.基本字符的匹配
SELECT * FROM a1 WHERE name regexp '1000' #匹配名称含有1000的所有行
SELECT * FROM a1 WHERE name regexp '.000' #匹配以000结尾的所有行,(.正则中表示:匹配任意一个字符)
从中可以看到正则表达式能够模拟LIKE使用通配符,注意:在通配符能完成的时候就不用正则,因为正则可能慢点,当然正则也能简化通配符,完成更大的作用。所以要有所取舍。
LIKE与REGEXP的区别:
SELECT * FROM a1 WHERE name LIKE 'a'
SELECT * FROM a1 WHERE name regexp 'a'
下面两条语句第一条不返回数据,第二条返回数据。
原因如下:
LIKE匹配整个列值时,不会找到它,相应的行也不会被返回(除非使用通配符)
REGEXP匹配时,会自动查找并返回结果。
那么REGEXP也能匹配整个列值,使用^和$定位符即可!
匹配不区分大小写
Mysql正则大小写都会匹配,为区分大小写可使用binary关键字,如:
SELECT * FROM a1 WHERE name LIKE binary '%J%' #使用LIKE+通配符匹配大写J
SELECT * FROM a1 WHERE name regexp binary 'j' #使用正则匹配小写j
[1] [2]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).
