MYSQL 查询命令

目录

一、概念

 二、语法

2-1:创建库  创建表 创建列

2-2插入表信息

 2-3 插入表值

三 DML   部分插入

四 更新 update

五 删除数据 delete

 六 查询

6-1条件查询

6-2 关键词查询

 6-3 升序降序查询


一、概念

 

 

 

 

 二、语法

 

 

2-1:创建库  创建表 创建列

查看库   查询库  

 

 

 

2-2插入表信息

 

 

 2-3 插入表值

 

 

三 DML   部分插入

mysql> select *from student1;
+------+-------+------+------+
| id   | name  | sex  | age  |
+------+-------+------+------+
|    1 | lisi  | m    |    4 |
|    2 | zahng | f    |   52 |
|    3 | ignng | f    |   23 |
+------+-------+------+------+
3 rows in set (0.00 sec)


mysql> insert into student1 (id,name)values(4,'xinjia');
Query OK, 1 row affected (0.02 sec)

mysql> select *from student1;
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | lisi   | m    |    4 |
|    2 | zahng  | f    |   52 |
|    3 | ignng  | f    |   23 |
|    4 | xinjia | NULL | NULL |
+------+--------+------+------+

四 更新 update

 示例1;

mysql> update student1 set name='gengxin' where id=5;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from student1;
+------+---------+------+------+
| id   | name    | sex  | age  |
+------+---------+------+------+
|    1 | lisi    | m    |    4 |
|    2 | zahng   | f    |   52 |
|    3 | ignng   | f    |   23 |
|    4 | xinjia  | NULL | NULL |
|    5 | gengxin | f    |   22 |
+------+---------+------+------+

示例2更改mysql root的密码;

        可以先进入mysql库  在进入tabel, 再查询user;  authentication_string;

也可以直接进入

 update mysql.user set authentication_string=password("ifan") where user="root";
Query OK, 4 rows affected, 1 warning (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 1
//直接进入mysql库 用户 root;
mysql> \q
Bye 

mysql> flush privileges;        //此时库密码还没更新,需要立刻更新
Query OK, 0 rows affected (0.01 sec)

mysql> \q
Bye

[root@localhost ~]# mysql -uroot -p'ifan';        //使用新密码
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

五 删除数据 delete

mysql> show databases;            //进库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| haha               |
| mysql              |
| performance_schema |
| school             |
+--------------------+
5 rows in set (0.00 sec)

mysql> use school;            //进表
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc tabels;            //查表数据值
ERROR 1146 (42S02): Table 'school.tabels' doesn't exist
mysql> desc student1;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id    | int(11)       | YES  |     | NULL    |       |
| name  | varchar(20)   | YES  |     | NULL    |       |
| sex   | enum('m','f') | YES  |     | NULL    |       |
| age   | int(11)       | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select *from student1;        //查库student1
+------+---------+------+------+
| id   | name    | sex  | age  |
+------+---------+------+------+
|    1 | lisi    | m    |    4 |
|    2 | zahng   | f    |   52 |
|    3 | ignng   | f    |   23 |
|    4 | xinjia  | NULL | NULL |
|    5 | gengxin | f    |   22 |
+------+---------+------+------+
5 rows in set (0.00 sec)

mysql> delect from student1 where id=5;        //删除库 id 5 一列
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 'delect from student1 where id=5' at line 1
mysql> delete from student1 where id=5;
Query OK, 1 row affected (0.00 sec)

mysql> select *from student1;
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | lisi   | m    |    4 |
|    2 | zahng  | f    |   52 |
|    3 | ignng  | f    |   23 |
|    4 | xinjia | NULL | NULL |
+------+--------+------+------+
4 rows in set (0.00 sec)

mysql> 

 六 查询

mysql> create database t1;
Query OK, 1 row affected (0.00 sec)


mysql> use t1;
Database changed

mysql> create   table   t3   (id   int,name   varchar(20),age  int);
Query OK, 0 rows affected (0.01 sec)

        insert   into   t3   values  (1,"zhangsan",23);
Query OK, 1 row affected (0.00 sec)

mysql> insert   into   t3   values  (2,"lisi",24);
Query OK, 1 row affected (0.00 sec)

mysql> insert   into   t3   values  (3,"wangwu",18);
Query OK, 1 row affected (0.00 sec)

mysql> select *from t3;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   23 |
|    2 | lisi     |   24 |
|    3 | wangwu   |   18 |
+------+----------+------+
3 rows in set (0.00 sec)

mysql> exit 
Bye
[root@localhost ~]# mysql -uroot -p'ifan';

mysql> create database company;
Query OK, 1 row affected (0.00 sec)

mysql>  CREATE TABLE company.employee5(
    ->      id int primary key AUTO_INCREMENT not null,
    ->     name varchar(30) not null,
    ->     sex enum('male','female') default 'male' not null,
    ->      hire_date date not null,
    ->      post varchar(50) not null,
    ->      job_description varchar(100),
    ->      salary double(15,2) not null,
    ->      office int,
    ->      dep_id int
    ->      );
Query OK, 0 rows affected (0.01 sec)


mysql> use company;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc employee5;
+-----------------+-----------------------+------+-----+---------+----------------+
| Field           | Type                  | Null | Key | Default | Extra          |
+-----------------+-----------------------+------+-----+---------+----------------+
| id              | int(11)               | NO   | PRI | NULL    | auto_increment |
| name            | varchar(30)           | NO   |     | NULL    |                |
| sex             | enum('male','female') | NO   |     | male    |                |
| hire_date       | date                  | NO   |     | NULL    |                |
| post            | varchar(50)           | NO   |     | NULL    |                |
| job_description | varchar(100)          | YES  |     | NULL    |                |
| salary          | double(15,2)          | NO   |     | NULL    |                |
| office          | int(11)               | YES  |     | NULL    |                |
| dep_id          | int(11)               | YES  |     | NULL    |                |
+-----------------+-----------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)


mysql> > insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values  ('jack','male','20180202','instructor','teach',5000,501,100), ('tom','male','20180203','instructor','teach',5500,501,100), ('robin','male','20180202','instructor','teach',8000,501,100), ('alice','female','20180202','instructor','teach',7200,501,100), ('aofa','male','20180202','hr','hrcc',600,502,101), ('harry','male','20180202','hr',NULL,6000,502,101), ('emma','female','20180206','sale','salecc',20000,503,102), ('christine','female','20180205','sale','salecc',2200,503,102), ('zhuzhu','male','20180205','sale',NULL,2200,503,102), ('gougou','male','20180205','sale','',2200,503,102);
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 '> insert into company.employee5(name,sex,hire_date,post,job_description,salary,o' at line 1
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values 
    -> ('jack','male','20180202','instructor','teach',5000,501,100),
    -> ('tom','male','20180203','instructor','teach',5500,501,100),
    -> ('robin','male','20180202','instructor','teach',8000,501,100),
    -> ('alice','female','20180202','instructor','teach',7200,501,100),
    -> ('aofa','male','20180202','hr','hrcc',600,502,101),
    -> ('harry','male','20180202','hr',NULL,6000,502,101),
    -> ('emma','female','20180206','sale','salecc',20000,503,102),
    -> ('christine','female','20180205','sale','salecc',2200,503,102),
    -> ('zhuzhu','male','20180205','sale',NULL,2200,503,102),
    -> ('gougou','male','20180205','sale','',2200,503,102);
Query OK, 10 rows affected (0.05 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select *from employee5;
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name      | sex    | hire_date  | post       | job_description | salary   | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|  1 | jack      | male   | 2018-02-02 | instructor | teach           |  5000.00 |    501 |    100 |
|  2 | tom       | male   | 2018-02-03 | instructor | teach           |  5500.00 |    501 |    100 |
|  3 | robin     | male   | 2018-02-02 | instructor | teach           |  8000.00 |    501 |    100 |
|  4 | alice     | female | 2018-02-02 | instructor | teach           |  7200.00 |    501 |    100 |
|  5 | aofa      | male   | 2018-02-02 | hr         | hrcc            |   600.00 |    502 |    101 |
|  6 | harry     | male   | 2018-02-02 | hr         | NULL            |  6000.00 |    502 |    101 |
|  7 | emma      | female | 2018-02-06 | sale       | salecc          | 20000.00 |    503 |    102 |
|  8 | christine | female | 2018-02-05 | sale       | salecc          |  2200.00 |    503 |    102 |
|  9 | zhuzhu    | male   | 2018-02-05 | sale       | NULL            |  2200.00 |    503 |    102 |
| 10 | gougou    | male   | 2018-02-05 | sale       |                 |  2200.00 |    503 |    102 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)

mysql> 

6-1条件查询

mysql> select name,post from employee5 where post='hr';    //***但条件查询where 
                                                          查询HR部门的员工****//
+-------+------+
| name  | post |
+-------+------+
| aofa  | hr   |
| harry | hr   |
+-------+------+
2 rows in set (0.00 sec)

mysql> select name,salary from employee5 where post'hr'and salary>1000;
//********多条件查询ADN/OR         查询HR部门员工姓名and 工资大于1000******//

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 ''hr'and salary>1000' at line 1
mysql> select name,salary from employee5 where post='hr'and salary>1000;
+-------+---------+            //*** 查询所有部门员工姓名,and工资大于1000***//
| name  | salary  |
+-------+---------+
| harry | 6000.00 |
+-------+---------+
1 row in set (0.00 sec)


mysql> select name,salary from  employee5 where salary=6000 or salary=8000;

//******查询所有员工工资6K或者8k*****//

+-------+---------+
| name  | salary  |
+-------+---------+
| robin | 8000.00 |
| harry | 6000.00 |
+-------+---------+
2 rows in set (0.01 sec)


mysql> select name,salary from employee5 where salary between 5000 and 15000;
//******查询所有员工  工资在5K---15000之间          between and******//
+-------+---------+
| name  | salary  |
+-------+---------+
| jack  | 5000.00 |
| tom   | 5500.00 |
| robin | 8000.00 |
| alice | 7200.00 |
| harry | 6000.00 |
+-------+---------+
5 rows in set (0.01 sec)

mysql> select *from employee5 where salary between 5000 and 15000;
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name  | sex    | hire_date  | post       | job_description | salary  | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|  1 | jack  | male   | 2018-02-02 | instructor | teach           | 5000.00 |    501 |    100 |
|  2 | tom   | male   | 2018-02-03 | instructor | teach           | 5500.00 |    501 |    100 |
|  3 | robin | male   | 2018-02-02 | instructor | teach           | 8000.00 |    501 |    100 |
|  4 | alice | female | 2018-02-02 | instructor | teach           | 7200.00 |    501 |    100 |
|  6 | harry | male   | 2018-02-02 | hr         | NULL            | 6000.00 |    502 |    101 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
5 rows in set (0.00 sec)


mysql> SELECT name,salary FROM employee5 
    -> WHERE salary NOT BETWEEN 5000 AND 15000;   //****查询工资不在5K---15000之间的****//
+-----------+----------+
| name      | salary   |
+-----------+----------+
| aofa      |   600.00 |
| emma      | 20000.00 |
| christine |  2200.00 |
| zhuzhu    |  2200.00 |
| gougou    |  2200.00 |
+-----------+----------+
5 rows in set (0.01 sec)

mysql> SELECT name, salary FROM employee5 
    -> WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ;
//***查询工资4k 5k 9k****//;
+-------+---------+
| name  | salary  |
+-------+---------+
| jack  | 5000.00 |
| harry | 6000.00 |
+-------+---------+
2 rows in set (0.00 sec)

  
mysql> SELECT name, salary FROM employee5  
WHERE salary IN (4000,5000,6000,9000);//**********查询工资在()的人*****//
+-------+---------+
| name  | salary  |
+-------+---------+
| jack  | 5000.00 |
| harry | 6000.00 |
+-------+---------+
2 rows in set (0.00 sec)



mysql>  SELECT name, salary FROM employee5  
WHERE salary NOT IN (4000,5000,6000,9000);  //********查询工资不在()中的*********//
+-----------+----------+
| name      | salary   |
+-----------+----------+
| tom       |  5500.00 |
| robin     |  8000.00 |
| alice     |  7200.00 |
| aofa      |   600.00 |
| emma      | 20000.00 |
| christine |  2200.00 |
| zhuzhu    |  2200.00 |
| gougou    |  2200.00 |
+-----------+----------+
8 rows in set (0.00 sec)

mysql> 

6-2 关键词查询

 

mysql> SELECT name,job_description FROM employee5         //***查询 空****//
    -> WHERE job_description IS NULL;
+--------+-----------------+
| name   | job_description |
+--------+-----------------+
| harry  | NULL            |
| zhuzhu | NULL            |
+--------+-----------------+
2 rows in set (0.00 sec)


 SELECT name,job_description FROM employee5 //*****查询非空*********//
    -> 
Display all 762 possibilities? (y or n) 
    -> WHERE job_description IS NOT NULL;
+-----------+-----------------+
| name      | job_description |
+-----------+-----------------+
| jack      | teach           |
| tom       | teach           |
| robin     | teach           |
| alice     | teach           |
| aofa      | hrcc            |
| emma      | salecc          |
| christine | salecc          |
| gougou    |                 |
+-----------+-----------------+
8 rows in set (0.00 sec)

 

 

 

mysql> select *from employee5;
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name      | sex    | hire_date  | post       | job_description | salary   | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|  1 | jack      | male   | 2018-02-02 | instructor | teach           |  5000.00 |    501 |    100 |
|  2 | tom       | male   | 2018-02-03 | instructor | teach           |  5500.00 |    501 |    100 |
|  3 | robin     | male   | 2018-02-02 | instructor | teach           |  8000.00 |    501 |    100 |
|  4 | alice     | female | 2018-02-02 | instructor | teach           |  7200.00 |    501 |    100 |
|  5 | aofa      | male   | 2018-02-02 | hr         | hrcc            |   600.00 |    502 |    101 |
|  6 | harry     | male   | 2018-02-02 | hr         | NULL            |  6000.00 |    502 |    101 |
|  7 | emma      | female | 2018-02-06 | sale       | salecc          | 20000.00 |    503 |    102 |
|  8 | christine | female | 2018-02-05 | sale       | salecc          |  2200.00 |    503 |    102 |
|  9 | zhuzhu    | male   | 2018-02-05 | sale       | NULL            |  2200.00 |    503 |    102 |
| 10 | gougou    | male   | 2018-02-05 | sale       |                 |  2200.00 |    503 |    102 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)

mysql> SELECT * FROM employee5        //***查询 名字带al的***//
    -> WHERE name LIKE 'al%';
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name  | sex    | hire_date  | post       | job_description | salary  | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|  4 | alice | female | 2018-02-02 | instructor | teach           | 7200.00 |    501 |    100 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
1 row in set (0.01 sec)

mysql> SELECT * FROM employee5 
    -> WHERE name LIKE 'al___';           //**查询名字后缀3 (下划线几个代表几个字符)****//
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name  | sex    | hire_date  | post       | job_description | salary  | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|  4 | alice | female | 2018-02-02 | instructor | teach           | 7200.00 |    501 |    100 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
1 row in set (0.00 sec)



mysql> SELECT * FROM employee5  WHERE name LIKE 'a____';
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name  | sex    | hire_date  | post       | job_description | salary  | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|  4 | alice | female | 2018-02-02 | instructor | teach           | 7200.00 |    501 |    100 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
1 row in set (0.00 sec)

mysql> 

 6-3 升序降序查询

 

mysql> SELECT    *     FROM    employee5   ORDER       BY   salary     ASC;//*升序**//
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name      | sex    | hire_date  | post       | job_description | salary   | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|  5 | aofa      | male   | 2018-02-02 | hr         | hrcc            |   600.00 |    502 |    101 |
| 10 | gougou    | male   | 2018-02-05 | sale       |                 |  2200.00 |    503 |    102 |
|  9 | zhuzhu    | male   | 2018-02-05 | sale       | NULL            |  2200.00 |    503 |    102 |
|  8 | christine | female | 2018-02-05 | sale       | salecc          |  2200.00 |    503 |    102 |
|  1 | jack      | male   | 2018-02-02 | instructor | teach           |  5000.00 |    501 |    100 |
|  2 | tom       | male   | 2018-02-03 | instructor | teach           |  5500.00 |    501 |    100 |
|  6 | harry     | male   | 2018-02-02 | hr         | NULL            |  6000.00 |    502 |    101 |
|  4 | alice     | female | 2018-02-02 | instructor | teach           |  7200.00 |    501 |    100 |
|  3 | robin     | male   | 2018-02-02 | instructor | teach           |  8000.00 |    501 |    100 |
|  7 | emma      | female | 2018-02-06 | sale       | salecc          | 20000.00 |    503 |    102 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)

mysql> SELECT    *     FROM    employee5   ORDER       BY   salary     DSC;//**降序**//
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 'DSC' at line 1
mysql> SELECT    *     FROM    employee5   ORDER       BY   salary     DESC;
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name      | sex    | hire_date  | post       | job_description | salary   | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|  7 | emma      | female | 2018-02-06 | sale       | salecc          | 20000.00 |    503 |    102 |
|  3 | robin     | male   | 2018-02-02 | instructor | teach           |  8000.00 |    501 |    100 |
|  4 | alice     | female | 2018-02-02 | instructor | teach           |  7200.00 |    501 |    100 |
|  6 | harry     | male   | 2018-02-02 | hr         | NULL            |  6000.00 |    502 |    101 |
|  2 | tom       | male   | 2018-02-03 | instructor | teach           |  5500.00 |    501 |    100 |
|  1 | jack      | male   | 2018-02-02 | instructor | teach           |  5000.00 |    501 |    100 |
|  8 | christine | female | 2018-02-05 | sale       | salecc          |  2200.00 |    503 |    102 |
|  9 | zhuzhu    | male   | 2018-02-05 | sale       | NULL            |  2200.00 |    503 |    102 |
| 10 | gougou    | male   | 2018-02-05 | sale       |                 |  2200.00 |    503 |    102 |
|  5 | aofa      | male   | 2018-02-02 | hr         | hrcc            |   600.00 |    502 |    101 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)


mysql> SELECT  * FROM employee5 ORDER  BY salary  DESC LImIT 5;        //**降序 前5***//
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name  | sex    | hire_date  | post       | job_description | salary   | office | dep_id |
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
|  7 | emma  | female | 2018-02-06 | sale       | salecc          | 20000.00 |    503 |    102 |
|  3 | robin | male   | 2018-02-02 | instructor | teach           |  8000.00 |    501 |    100 |
|  4 | alice | female | 2018-02-02 | instructor | teach           |  7200.00 |    501 |    100 |
|  6 | harry | male   | 2018-02-02 | hr         | NULL            |  6000.00 |    502 |    101 |
|  2 | tom   | male   | 2018-02-03 | instructor | teach           |  5500.00 |    501 |    100 |
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
5 rows in set (0.00 sec)

mysql> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值