mysql 5.7 增删改查及别名的用法

1.启动和停止服务

一)启动和停止

#启动服务: 
$sudo service mysql start 
#停止服务: 
$sudo service mysql stop 

二)创建和选择数据库

【创建数据库】

mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)

mysql> create database testdb default character set 'utf8';
Query OK, 1 row affected (0.00 sec)

status命令查看数据库状态:

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.7.21, for Linux (i686) using  EditLine wrapper

Connection id:        13
Current database:    
Current user:        root@localhost
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server version:        5.7.21 MySQL Community Server (GPL)
Protocol version:    10
Connection:        Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /var/run/mysqld/mysqld.sock
Uptime:            5 hours 28 min 6 sec

Threads: 1  Questions: 211  Slow queries: 0  Opens: 123  Flush tables: 1  Open tables: 116  Queries per second avg: 0.010

【选择数据库】

mysql> use testdb;
Database changed

 【查看当前登录的数据库】

mysql> select database() mydb;
+--------+
| mydb   |
+--------+
| testdb |
+--------+
1 row in set (0.00 sec)
mysql> desc t_users;
+----------+-------------+------+-----+-------------------+----------------+
| Field    | Type        | Null | Key | Default           | Extra          |
+----------+-------------+------+-----+-------------------+----------------+
| id       | int(11)     | NO   | PRI | NULL              | auto_increment |
| name     | varchar(20) | NO   |     | NULL              |                |
| owner    | varchar(20) | NO   |     | NULL              |                |
| descinfo | varchar(50) | YES  |     | NULL              |                |
| sex      | char(1)     | NO   |     | 1                 |                |
| birth    | date        | YES  |     | NULL              |                |
| death    | date        | YES  |     | NULL              |                |
| addtime  | datetime    | YES  |     | CURRENT_TIMESTAMP |                |
+----------+-------------+------+-----+-------------------+----------------+
8 rows in set (0.00 sec)

 

【删除数据库】

mysql> drop database testdb;
Query OK, 0 rows affected (0.00 sec)

 

【创建用户和分配权限】

mysql> grant all privileges  on testdb.* to 'test2'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| %         | root          | *9AAE89836D1D658A0BED88FEB96A3DFF84C94D77 |
| %         | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | test          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | test2         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+---------------+-------------------------------------------+
5 rows in set (0.00 sec)

【创建表】

从源文档引入创建:假设有个sql.txt

DROP TABLE IF EXISTS t_users;
create table t_users (
id int(11) not null auto_increment,
name varchar(20) not null comment '姓名',
owner varchar(20) not null,
descinfo varchar(50),
sex char(1) not null default '1',
birth date comment '生日',
death date,
addtime datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
primary key (id)
);

 

然后执行:

mysql> source ./sql.txt;
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| t_users          |
+------------------+
1 row in set (0.00 sec)

mysql> desc t_users;
+----------+-------------+------+-----+-------------------+----------------+
| Field    | Type        | Null | Key | Default           | Extra          |
+----------+-------------+------+-----+-------------------+----------------+
| id       | int(11)     | NO   | PRI | NULL              | auto_increment |
| name     | varchar(20) | NO   |     | NULL              |                |
| owner    | varchar(20) | NO   |     | NULL              |                |
| descinfo | varchar(50) | YES  |     | NULL              |                |
| sex      | char(1)     | NO   |     | 1                 |                |
| birth    | date        | YES  |     | NULL              |                |
| death    | date        | YES  |     | NULL              |                |
| addtime  | datetime    | YES  |     | CURRENT_TIMESTAMP |                |
+----------+-------------+------+-----+-------------------+----------------+
8 rows in set (0.00 sec)

mysql> insert into t_users (name,owner,descinfo,birth,death) values ('php webs','test webs','test ok next boooks',curdate(),curdate());
Query OK, 1 row affected (0.01 sec)

mysql> select t.* from t_users t;
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
| id | name     | owner     | descinfo            | sex | birth      | death      | addtime             |
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
|  1 | php webs | test webs | test ok next boooks | 1   | 2018-04-20 | 2018-04-20 | 2018-04-20 16:40:59 |
+----+----------+-----------+---------------------+-----+------------+------------+---------------------+
1 row in set (0.00 sec)

 

2.删掉时候用别名

delete t from t_users t where t.title='John';

 3.查询及排序 

默认asc升序 降序desc

SELECT name, birth FROM pet ORDER BY birth

结果:

 

查询当前时间和mysql版本函数 curdate() 和 current_date:

mysql> select version(),curdate();
+-----------+------------+
| version() | curdate()  |
+-----------+------------+
| 5.7.21    | 2018-04-20 |
+-----------+------------+
1 row in set (0.00 sec)
mysql> select version(),current_date;
+-----------+--------------+
| version() | current_date |
+-----------+--------------+
| 5.7.21    | 2018-04-20   |
+-----------+--------------+
1 row in set (0.00 sec)

now(): 函数查询当前时间

mysql> select version(),now();
+-----------+---------------------+
| version() | now()               |
+-----------+---------------------+
| 5.7.21    | 2018-04-20 15:53:04 |
+-----------+---------------------+
1 row in set (0.00 sec)

 

TIMESTAMPDIFF (year,birth,curdate()):日期计算使用 函数计算 查询年龄:

mysql> SELECT name, birth, CURDATE(),TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age FROM pet;

结果:

 is not null 查询不为空的数据:

mysql> select t.* from t_users t where t.title is not null;
+----+------------+--------------+-----------------+
| id | title      | author       | submission_date |
+----+------------+--------------+-----------------+
|  1 | 学习 PHP   | 菜鸟教程     | 2018-04-11      |
| 31 | PHP webs   | Doe          | 2018-04-16      |
+----+------------+--------------+-----------------+
2 rows in set (0.00 sec)

day()  和month()函数的使用:查询指定日期的数据:

mysql> select t.* from t_users t where day(t.submission_date)=11;
+----+------------+--------------+-----------------+
| id | title      | author       | submission_date |
+----+------------+--------------+-----------------+
|  1 | 学习 PHP   | 菜鸟教程     | 2018-04-11      |
+----+------------+--------------+-----------------+
1 row in set (0.01 sec)


mysql> select t.* from t_users t where month(t.submission_date)=4;
+----+------------+--------------+-----------------+
| id | title      | author       | submission_date |
+----+------------+--------------+-----------------+
|  1 | 学习 PHP   | 菜鸟教程     | 2018-04-11      |
| 31 | PHP webs   | Doe          | 2018-04-16      |
+----+------------+--------------+-----------------+
2 rows in set (0.00 sec)

mod(n,m) 取余函数的使用

查询下一个月:

mysql> select mod(month(curdate()),12)+1 as nextmonth;
+-----------+
| nextmonth |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)

如:想要查询下一个月过生日的人;

mysql> select t.* from t_users where t.month(birth) =  mod(month(curdate()),12)+1 ;

1 is null 返回false;

1 is not null 返回 true;

mysql> select 1 is not null;
+---------------+
| 1 is not null |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

利用count()函数统计数据 以owner分组 group by

mysql> select owner,count(name) from t_users where name like 'C%' group by owner;
+----------+-------------+
| owner    | count(name) |
+----------+-------------+
| C webs   |           5 |
| GOGGGOOK |           2 |
| GOOOK    |           1 |
+----------+-------------+
3 rows in set (0.00 sec)

 

max(col):查询某列的最大值

mysql> select max(id) from t_users;
+---------+
| max(id) |
+---------+
|      17 |
+---------+
1 row in set (0.00 sec)

【统计每天访问量】

mysql> select t.* from t1 t;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |     1 |    1 |
| 2000 |     1 |   20 |
| 2000 |     1 |   30 |
| 2000 |     2 |    2 |
| 2000 |     2 |   22 |
| 2000 |     2 |   23 |
| 2000 |     2 |   22 |
+------+-------+------+
7 rows in set (0.01 sec)

mysql> select year,month,bit_count(bit_or(1<<day)) as days from t1 group by year,month;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 |     1 |    3 |
| 2000 |     2 |    3 |
+------+-------+------+
2 rows in set (0.00 sec)

 

4.Mysql使用正则表达式匹配模糊查询 like的使用

Mysql提供了一个标准的SQL模式匹配,和基于扩展的正则表达式的模式匹配Unix工具(如vi,grep,sed)一样。
SQL模式匹配可以使用:

“_“来匹配任意单个字符,

”%“可以用来匹配任意数量(包含0个字符)的字符。

在MySQL中,SQL模式匹配的大小写默认是不敏感的,以下有一些例子,当你在使用SQL模式时,不要使用=或<>,而是使用LIKE或NOTLIKE。

 要找到P%开头的名字:

mysql> select t.* from t_users t where t.title like 'P%'
    -> ;
+----+----------+--------+-----------------+
| id | title    | author | submission_date |
+----+----------+--------+-----------------+
| 31 | PHP webs | Doe    | 2018-04-16      |
+----+----------+--------+-----------------+
1 row in set (0.00 sec)

查询以%P结尾的名字:

mysql> select t.* from t_users t where t.title like '%PHP';
+----+------------+--------------+-----------------+
| id | title      | author       | submission_date |
+----+------------+--------------+-----------------+
|  1 | 学习 PHP   | 菜鸟教程     | 2018-04-11      |
+----+------------+--------------+-----------------+
1 row in set (0.00 sec)

使用“_”匹配 4个字符的名字:

mysql> select t.* from t_users t where t.title like '____';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| 32 | John  | Doe    | 2018-04-20      |
| 33 | John  | Doe    | 2018-04-20      |
| 34 | John  | Doe    | 2018-04-20      |
| 35 | John  | Doe    | 2018-04-20      |
| 36 | John  | Doe    | 2018-04-20      |
| 37 | John  | Doe    | 2018-04-20      |
+----+-------+--------+-----------------+
6 rows in set (0.00 sec)

 5.利用正则表达式REGEXP 和 NOT REGEXP

MySQL提供的其他模式匹配类型是使用扩展的正则表达式,当你使用这个类型来测试一个匹配,
要使用REGEXP和NOT REGEXP操作(或者RLIKE和NOT RLIKE,他们是同意词)。
下面的列表描述了一些扩展正则表达式的特征:

"."匹配任意单个字符.

[a-z]:单个字符匹配类"[...]"匹配括号里的任意字符,例如,"[abc]"匹配"a","b",或"c",要指定字符范围,可以使用"-",
例如,[a-z] 匹配任意字母,而[0-9]匹配任意数字。

"*" 匹配0个或多个在它前面的东西。例如,"x*" 匹配任意个x字符,"[0-9]*"匹配任意个数字,".*"匹配任意个字符。
一个REGEXP模式匹配成功的条件是,模式在测试值中的任意地方匹配即可。
(这与LIKE模式匹配不同,LIKE模式匹配成功需要匹配整个值)。
"^" 开头作为模式匹配或用
"$" 作为模式的结尾。

为了验证扩展的正则表达式是如何工作的,在这使用REGEXP对前面使用LIKE查询进行重写。

查询以P开头:

mysql> select * from t_users where title regexp '^P';
+----+----------+--------+-----------------+
| id | title    | author | submission_date |
+----+----------+--------+-----------------+
| 31 | PHP webs | Doe    | 2018-04-16      |
+----+----------+--------+-----------------+
1 row in set (0.00 sec)

查询以s结尾:

mysql> select * from t_users where title regexp 's$';
+----+----------+--------+-----------------+
| id | title    | author | submission_date |
+----+----------+--------+-----------------+
| 31 | PHP webs | Doe    | 2018-04-16      |
+----+----------+--------+-----------------+
1 row in set (0.00 sec)

查询title包含e字符的:

mysql> select * from t_users where title regexp 'e';
+----+------------+----------+-----------------+
| id | title      | author   | submission_date |
+----+------------+----------+-----------------+
| 31 | PHP webs   | Doe      | 2018-04-16      |
| 38 | nexJohn    | Doe      | 2018-04-20      |
| 39 | nexJohn123 | Doe baok | 2018-04-20      |
+----+------------+----------+-----------------+
3 rows in set (0.00 sec)

利用...匹配查询4个字符的名称:如查询以J开头的 并且是...表示三个字符

如:要找到只包含3个字符的名字,使用“^"和“$"来匹配名字的开头和结尾,且放3个“."在中间

mysql> select * from t_users where title regexp '^J...$';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| 32 | John  | Doe    | 2018-04-20      |
| 33 | John  | Doe    | 2018-04-20      |
| 34 | John  | Doe    | 2018-04-20      |
| 35 | John  | Doe    | 2018-04-20      |
| 36 | John  | Doe    | 2018-04-20      |
| 37 | John  | Doe    | 2018-04-20      |
+----+-------+--------+-----------------+
6 rows in set (0.00 sec)

利用{n} ;n表示重复次数来替换上面的语句查询:

mysql> select * from t_users where title regexp '^.{4}$';
+----+-------+--------+-----------------+
| id | title | author | submission_date |
+----+-------+--------+-----------------+
| 32 | John  | Doe    | 2018-04-20      |
| 33 | John  | Doe    | 2018-04-20      |
| 34 | John  | Doe    | 2018-04-20      |
| 35 | John  | Doe    | 2018-04-20      |
| 36 | John  | Doe    | 2018-04-20      |
| 37 | John  | Doe    | 2018-04-20      |
+----+-------+--------+-----------------+
6 rows in set (0.00 sec)

 

关键词:mysql gt mysql> sec 匹配 set select doe null t_users

相关推荐:

mysql-数据(记录)相关操作(增删改查)及权限管理

MySQL从零开始:04 表的增删改查

Mysql数据库(五)表记录的检索

数据库---表操作---增删改查

Mysql数据库(四)表记录的更新操作

数据库 - 表 - 增删改查

Mysql数据库(七)触发器

表记录曾删改查

Mysql数据库(八)存储过程与存储函数