MySQL 查询语句:
1、查询五子句:(重点)
mysql> select */字段列表 from 数据表名称 where 子句 group by 子句 having 子句 order by 子句 limit 子句;
1)where 子句;条件筛选。
2)group by 子句;分组子句;统计学中的概念
3)having 子句; having是放在分组之后的,跟where有点类似。
4)order by 子句;排序子句;按年龄从大到小的排序。
5)limit 子句;限制条件的,例如:前三名同学的信息。
注意:五子句的顺序是固定的。不能颠倒。顺序错了,MySQL就会报错。
1)Where子句:
案例1: like模糊查询语句,
准备查询:
mysql> create table tb_student(-> id mediumint not null auto_increment,-> name varchar(20),-> age tinyint unsigned default 0,-> gender enum('男','女'),-> address varchar(255),-> primary key(id)-> ) engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)mysql> insert into tb_student values (null,'刘备',33,'男','湖北省武汉市');
Query OK, 1 row affected (0.01 sec)mysql> insert into tb_student values (null,'貂蝉',18,'女','湖南省长沙市');
Query OK, 1 row affected (0.00 sec)mysql> insert into tb_student values (null,'关羽',32,'男','湖北省荆州市');
Query OK, 1 row affected (0.00 sec)mysql> insert into tb_student values (null,'大乔',20,'女','河南省漯河市');
Query OK, 1 row affected (0.00 sec)mysql> insert into tb_student values (null,'赵云',25,'男','河北省石家庄市');
Query OK, 1 row affected (0.01 sec)mysql>
mysql> insert into tb_student values (null,'小乔',18,'女','湖北省荆州市');
Query OK, 1 row affected (0.00 sec)mysql> select * from tb_student;
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+-----------------------+
6 rows in set (0.00 sec)
查询姓"关"的同学信息(name字段对应值应该以"关"开头)
mysql> select * from tb_student where name like '关%';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
1 row in set (0.00 sec)
like是模糊匹配。%是匹配0个或者多个字符。_下划线匹配单个字符。
like 有点类似Linux的grep命令。
案例:like模糊查询语句,查询名字中带"蝉"字的同学信息
mysql> select * from tb_student where name like '%蝉%';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
+----+--------+------+--------+--------------------+
1 row in set (0.00 sec)
案例:like模糊查询语句,查询云字结尾,且名字为2个字的同学信息。
mysql> select * from tb_student where name like '_云';
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
+----+--------+------+--------+-----------------------+
1 row in set (0.00 sec)
案例:获取学生表中id号为3的同学信息。
mysql> select * from tb_student where id=3;
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
1 row in set (0.04 sec)
案例:获取年龄大于25的学生信息。
mysql> select * from tb_student where age > 25;
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
2 rows in set (0.00 sec)
案例:获取学生表中,性别不为男的同学信息(获取女同学的信息。)
mysql> select * from tb_student where gender != '男';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
3 rows in set (0.00 sec)mysql> select * from tb_student where gender <> '男';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
3 rows in set (0.00 sec)
逻辑运算符:
案例:获取学生表中,年龄大于30岁的男同学的信息。
mysql> select * from tb_student where age > 30 and gender = '男';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
2 rows in set (0.04 sec)mysql> select * from tb_student where age > 30 && gender = '男';
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
2 rows in set (0.00 sec)
案例:获取id为1/3/5的同学信息。
mysql> select * from tb_student where id = 1 or id = 3 or id=5;
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
+----+--------+------+--------+-----------------------+
3 rows in set (0.00 sec)
区间:
案例:获取年龄在18周岁~25周岁之间的同学信息。
mysql> select * from tb_student where age between 18 and 25;
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+-----------------------+
4 rows in set (0.00 sec)mysql> select * from tb_student where age >= 18 && age <= 25;
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+-----------------------+
4 rows in set (0.00 sec)
in和not in:
mysql> select * from tb_student where id in (2, 4, 6);
+----+--------+------+--------+--------------------+
| id | name | age | gender | address |
+----+--------+------+--------+--------------------+
| 2 | 貂蝉 | 18 | 女 | 湖南省长沙市 |
| 4 | 大乔 | 20 | 女 | 河南省漯河市 |
| 6 | 小乔 | 18 | 女 | 湖北省荆州市 |
+----+--------+------+--------+--------------------+
3 rows in set (0.00 sec)mysql> select * from tb_student where id not in (2, 4, 6);
+----+--------+------+--------+-----------------------+
| id | name | age | gender | address |
+----+--------+------+--------+-----------------------+
| 1 | 刘备 | 33 | 男 | 湖北省武汉市 |
| 3 | 关羽 | 32 | 男 | 湖北省荆州市 |
| 5 | 赵云 | 25 | 男 | 河北省石家庄市 |
+----+--------+------+--------+-----------------------+
3 rows in set (0.00 sec)