单表查询
Posted wom1999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单表查询相关的知识,希望对你有一定的参考价值。
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
查询数据
| 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换到最后
| 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表中的数据
| 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的位置
| gender | name |
+--------+----------+
| na | wangwu |
| nv | zhangsan |
| na | qingming |
| na | yuying |
| nv | zhansa |
| na | youqian |
+--------+----------+
6 rows in set
常见关系运算符有:
例如:查询student表中id=4的学生的姓名
| id | name |
+----+--------+
| 4 | yuying |
+----+--------+
1 row in set
| name | gender |
+--------+--------+
| wangwu | na |
+--------+--------+
1 row in set
| name |
+----------+
| zhangsan |
| qingming |
| yuying |
+----------+
3 rows in set
例如:查询表中id为1 2 3的记录
| id | grade | name | gender |
+----+-------+----------+--------+
| 1 | 40 | wangwu | na |
| 2 | 100 | zhangsan | nv |
| 3 | 90 | qingming | na |
+----+-------+----------+--------+
3 rows in set
| id | grade | name | gender |
+----+-------+---------+--------+
| 4 | 89 | yuying | na |
| 5 | 60 | zhansa | nv |
| 6 | 77 | youqian | na |
+----+-------+---------+--------+
3 rows in set
例如:查询表中id在2-5之间的学生姓名
| id | name |
+----+----------+
| 2 | zhangsan |
| 3 | qingming |
| 4 | yuying |
| 5 | zhansa |
+----+----------+
4 rows in set
| id | name |
+----+---------+
| 1 | wangwu |
| 6 | youqian |
+----+---------+
2 rows in set
以上是关于单表查询的主要内容,如果未能解决你的问题,请参考以下文章