标签:war val image idt warnings mic database dup ble
1:简单查询
SELECT语句
1.1:查询所有字段
eg1:查询student表中的所有的记录
创建一个student数据表
mysql> USE itcast;
Database changed
mysql> CREATE TABLE student(id INT(3) PRIMARY KEY AUTO_INCREMENT,name VARCHAR(20) NOT NULL,grade FLOAT,gender CHAR(2));
Query OK, 0 rows affected
用INSERT语句插入数据
mysql> INSERT INTO student(name,grade,gender) VALUES(‘wangwu‘,40,‘na‘),(‘zhangsan‘,100,‘nv‘),(‘qingming‘,90,‘na‘),(‘yuying‘,89,‘na‘),(‘zhansa‘,60,‘nv‘),(‘youqian‘,77,‘na‘);
Query OK, 6 rows affected
Records: 6 Duplicates: 0 Warnings: 4
查询数据
mysql> SELECT id,name,grade,gender FROM student;
+----+----------+-------+--------+
| id | name | grade | gender |
+----+----------+-------+--------+
| 1 | wangwu | 40 | na |
| 2 | zhangsan | 100 | nv |
| 3 | qingming | 90 | na |
| 4 | yuying | 89 | na |
| 5 | zhansa | 60 | nv |
| 6 | youqian | 77 | na |
+----+----------+-------+--------+
6 rows in set
例如:把name换到最后
mysql> SELECT id,grade,gender,name FROM student;
+----+-------+--------+----------+
| id | grade | gender | name |
+----+-------+--------+----------+
| 1 | 40 | na | wangwu |
| 2 | 100 | nv | zhangsan |
| 3 | 90 | na | qingming |
| 4 | 89 | na | yuying |
| 5 | 60 | nv | zhansa |
| 6 | 77 | na | youqian |
+----+-------+--------+----------+
6 rows in set
例如:用SELECT *FROM 查询student表中的数据
mysql> SELECT * FROM student;
+----+----------+-------+--------+
| id | name | grade | gender |
+----+----------+-------+--------+
| 1 | wangwu | 40 | na |
| 2 | zhangsan | 100 | nv |
| 3 | qingming | 90 | na |
| 4 | yuying | 89 | na |
| 5 | zhansa | 60 | nv |
| 6 | youqian | 77 | na |
+----+----------+-------+--------+
6 rows in set
1.2:查询指定字段
eg2:
查询结果
mysql> SELECT name,gender FROM student;
+----------+--------+
| name | gender |
+----------+--------+
| wangwu | na |
| zhangsan | nv |
| qingming | na |
| yuying | na |
| zhansa | nv |
| youqian | na |
+----------+--------+
6 rows in set
注意:改变字段位置,所显示的结果位置也会改变,
例如:改变name和gender的位置
mysql> SELECT gender,name FROM student;
+--------+----------+
| gender | name |
+--------+----------+
| na | wangwu |
| nv | zhangsan |
| na | qingming |
| na | yuying |
| nv | zhansa |
| na | youqian |
+--------+----------+
6 rows in set
2:按条件查询
2.1:关系运算符的查询
常见关系运算符有:
例如:查询student表中id=4的学生的姓名
mysql> SELECT id,name FROM student WHERE id=4;
+----+--------+
| id | name |
+----+--------+
| 4 | yuying |
+----+--------+
1 row in set
例如:查询name为“wangwu”的学生的性别
mysql> SELECT name,gender FROM student WHERE name=‘wangwu‘;
+--------+--------+
| name | gender |
+--------+--------+
| wangwu | na |
+--------+--------+
1 row in set
例如:查询表中grade大于80的学生姓名
mysql> SELECT name FROM student WHERE grade>80;
+----------+
| name |
+----------+
| zhangsan |
| qingming |
| yuying |
+----------+
3 rows in set
2.2:带IN关键字的查询
例如:查询表中id为1 2 3的记录
mysql> SELECT id,grade,name,gender FROM student WHERE id IN(1,2,3);
+----+-------+----------+--------+
| id | grade | name | gender |
+----+-------+----------+--------+
| 1 | 40 | wangwu | na |
| 2 | 100 | zhangsan | nv |
| 3 | 90 | qingming | na |
+----+-------+----------+--------+
3 rows in set
例如:查询表中id不为1 2 3的记录
mysql> SELECT id,grade,name,gender FROM student WHERE id NOT IN(1,2,3);
+----+-------+---------+--------+
| id | grade | name | gender |
+----+-------+---------+--------+
| 4 | 89 | yuying | na |
| 5 | 60 | zhansa | nv |
| 6 | 77 | youqian | na |
+----+-------+---------+--------+
3 rows in set
2.3:带BETWEEN AND关键字的查询
例如:查询表中id在2-5之间的学生姓名
mysql> SELECT id,name FROM student WHERE id BETWEEN 2 AND 5;
+----+----------+
| id | name |
+----+----------+
| 2 | zhangsan |
| 3 | qingming |
| 4 | yuying |
| 5 | zhansa |
+----+----------+
4 rows in set
例如:查询表中id不在2-5之间的学生姓名
mysql> SELECT id,name FROM student WHERE id NOT BETWEEN 2 AND 5;
+----+---------+
| id | name |
+----+---------+
| 1 | wangwu |
| 6 | youqian |
+----+---------+
2 rows in set
单表查询
标签:war val image idt warnings mic database dup ble
原文地址:https://www.cnblogs.com/wom1999/p/11890507.html