【MYSQL】数据库的一些基本操作(复习用)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| z1                 |
+--------------------+
5 rows in set (0.00 sec)

mysql> use z1
Database changed
mysql> show tables;
+--------------+
| Tables_in_z1 |
+--------------+
| customer     |
| exam         |
| goods        |
| purchase     |
+--------------+
4 rows in set (0.00 sec)

mysql> desc exam
    -> ;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int          | YES  |     | NULL    |       |
| name    | varchar(20)  | YES  |     | NULL    |       |
| chinese | decimal(4,1) | YES  |     | NULL    |       |
| math    | decimal(4,1) | YES  |     | NULL    |       |
| english | decimal(4,1) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select * from exam limit 5;
+------+------+---------+------+---------+
| id   | name | chinese | math | english |
+------+------+---------+------+---------+
|    1 | z1   |    23.0 | 23.0 |    23.0 |
|    2 | z2   |    49.0 | 54.0 |    65.0 |
|    3 | z3   |    87.0 | 54.0 |    34.0 |
|    4 | z4   |    58.0 | 80.0 |    90.0 |
|    5 | z5   |    78.0 | 88.0 |    98.0 |
+------+------+---------+------+---------+
5 rows in set (0.00 sec)

mysql> select * from exam limit 5 offset 5;
+------+------+---------+-------+---------+
| id   | name | chinese | math  | english |
+------+------+---------+-------+---------+
|    6 | z6   |    99.0 | 100.0 |   100.0 |
|    7 | z7   |    13.0 |  46.0 |    70.0 |
|    8 | z8   |    87.0 |  67.0 |    90.0 |
|    9 | z9   |    45.0 |  23.0 |    54.0 |
|   10 | q1   |    45.0 |  76.0 |    54.0 |
+------+------+---------+-------+---------+
5 rows in set (0.00 sec)

mysql> select * from exam where name like 'g%';
+------+-------+---------+------+---------+
| id   | name  | chinese | math | english |
+------+-------+---------+------+---------+
|   17 | g1    |    32.0 | 45.0 |    76.0 |
|   18 | g2434 |    21.0 | 24.0 |    45.0 |
|   19 | g3    |    34.0 | 32.0 |    99.0 |
+------+-------+---------+------+---------+
3 rows in set (0.00 sec)

mysql> select * from exam where name like 'g_';
+------+------+---------+------+---------+
| id   | name | chinese | math | english |
+------+------+---------+------+---------+
|   17 | g1   |    32.0 | 45.0 |    76.0 |
|   19 | g3   |    34.0 | 32.0 |    99.0 |
+------+------+---------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from exam where math between 40 and 70;
+------+------+---------+------+---------+
| id   | name | chinese | math | english |
+------+------+---------+------+---------+
|    2 | z2   |    49.0 | 54.0 |    65.0 |
|    3 | z3   |    87.0 | 54.0 |    34.0 |
|    7 | z7   |    13.0 | 46.0 |    70.0 |
|    8 | z8   |    87.0 | 67.0 |    90.0 |
|   14 | r2   |    43.0 | 65.0 |    75.0 |
|   16 | v2   |    76.0 | 45.0 |    87.0 |
|   17 | g1   |    32.0 | 45.0 |    76.0 |
+------+------+---------+------+---------+
7 rows in set (0.00 sec)

mysql> select name,id from exam where math between 40 and 70;
+------+------+
| name | id   |
+------+------+
| z2   |    2 |
| z3   |    3 |
| z7   |    7 |
| z8   |    8 |
| r2   |   14 |
| v2   |   16 |
| g1   |   17 |
+------+------+
7 rows in set (0.00 sec)

mysql> select name,chinese+english+math as total from exam where math between 40 and 70;
+------+-------+
| name | total |
+------+-------+
| z2   | 168.0 |
| z3   | 175.0 |
| z7   | 129.0 |
| z8   | 244.0 |
| r2   | 183.0 |
| v2   | 208.0 |
| g1   | 153.0 |
+------+-------+
7 rows in set (0.00 sec)

mysql> select name,chinese+english+math as total from exam where math is null;
+------+-------+
| name | total |
+------+-------+
| t54  |  NULL |
| gdwe |  NULL |
+------+-------+
2 rows in set (0.00 sec)

mysql> select * from exam where math is null;
+------+------+---------+------+---------+
| id   | name | chinese | math | english |
+------+------+---------+------+---------+
|   21 | t54  |    NULL | NULL |    NULL |
|   22 | gdwe |    32.0 | NULL |    NULL |
+------+------+---------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from exam where math is not null;
+------+-------+---------+-------+---------+
| id   | name  | chinese | math  | english |
+------+-------+---------+-------+---------+
|    1 | z1    |    23.0 |  23.0 |    23.0 |
|    2 | z2    |    49.0 |  54.0 |    65.0 |
|    3 | z3    |    87.0 |  54.0 |    34.0 |
|    4 | z4    |    58.0 |  80.0 |    90.0 |
|    5 | z5    |    78.0 |  88.0 |    98.0 |
|    6 | z6    |    99.0 | 100.0 |   100.0 |
|    7 | z7    |    13.0 |  46.0 |    70.0 |
|    8 | z8    |    87.0 |  67.0 |    90.0 |
|    9 | z9    |    45.0 |  23.0 |    54.0 |
|   10 | q1    |    45.0 |  76.0 |    54.0 |
|   11 | q2    |    78.0 |  87.0 |    34.0 |
|   12 | q3    |    99.0 |  88.0 |    77.0 |
|   13 | r1    |    76.0 |  32.0 |    54.0 |
|   14 | r2    |    43.0 |  65.0 |    75.0 |
|   15 | v1    |    34.0 |  34.0 |    43.0 |
|   16 | v2    |    76.0 |  45.0 |    87.0 |
|   17 | g1    |    32.0 |  45.0 |    76.0 |
|   18 | g2434 |    21.0 |  24.0 |    45.0 |
|   19 | g3    |    34.0 |  32.0 |    99.0 |
|   20 | y1    |    56.0 |  76.0 |    76.0 |
+------+-------+---------+-------+---------+
20 rows in set (0.00 sec)

mysql> select * from exam where math is not null and math <= 50;
+------+-------+---------+------+---------+
| id   | name  | chinese | math | english |
+------+-------+---------+------+---------+
|    1 | z1    |    23.0 | 23.0 |    23.0 |
|    7 | z7    |    13.0 | 46.0 |    70.0 |
|    9 | z9    |    45.0 | 23.0 |    54.0 |
|   13 | r1    |    76.0 | 32.0 |    54.0 |
|   15 | v1    |    34.0 | 34.0 |    43.0 |
|   16 | v2    |    76.0 | 45.0 |    87.0 |
|   17 | g1    |    32.0 | 45.0 |    76.0 |
|   18 | g2434 |    21.0 | 24.0 |    45.0 |
|   19 | g3    |    34.0 | 32.0 |    99.0 |
+------+-------+---------+------+---------+
9 rows in set (0.00 sec)

mysql> select * from exam where math <=> null;
+------+------+---------+------+---------+
| id   | name | chinese | math | english |
+------+------+---------+------+---------+
|   21 | t54  |    NULL | NULL |    NULL |
|   22 | gdwe |    32.0 | NULL |    NULL |
+------+------+---------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from exam where math = null;
Empty set (0.00 sec)

mysql> select * from exam where math<=50 and math not 45;
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 '45' at line 1
mysql> select * from exam where math<=50 and math is not 45;
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 '45' at line 1
mysql> select * from exam where math<=50 and math != 45;
+------+-------+---------+------+---------+
| id   | name  | chinese | math | english |
+------+-------+---------+------+---------+
|    1 | z1    |    23.0 | 23.0 |    23.0 |
|    7 | z7    |    13.0 | 46.0 |    70.0 |
|    9 | z9    |    45.0 | 23.0 |    54.0 |
|   13 | r1    |    76.0 | 32.0 |    54.0 |
|   15 | v1    |    34.0 | 34.0 |    43.0 |
|   18 | g2434 |    21.0 | 24.0 |    45.0 |
|   19 | g3    |    34.0 | 32.0 |    99.0 |
+------+-------+---------+------+---------+
7 rows in set (0.00 sec)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值