前面我们学会了如何在查询表的基础上增加一些简单的限定条件,比如查询单个列,查询多个列,查询结果去重,限制结果集记录条数,查询结果条件。但我们在实际操作中比如我们想查某一个人的信息又怎么办呢?
简单搜索条件:
我们需要把搜索条件放在where子句中,然后将where子句放到from子句后面。比如这样:
mysql> select * from student_info where name = '三玖';
+-----------+------+------+-----------+---------------------+-------+-----------------+
| number | name | sex | id_number | department | major | enrollment_time |
+-----------+------+------+-----------+---------------------+-------+-----------------+
| 220101103 | 三玖 | 女 | 123456787 | school of computing | jike | 2022-09-01 |
+-----------+------+------+-----------+---------------------+-------+-----------------+
1 row in set (0.01 sec)
搜索条件中“=”称为等于运算符,是比较运算符中的一种。下面介绍几个其他的比较运算符。
a=b(a等于b)
a<=>b(a等于b,但是这个等于是NULL值安全等于运算符)
a<>b(a不等于b)或者说a!=b
a
a<=b(a小于等于b)
a between b and c(a的值必须满足b<=a<=c)
a not between b and c(a的值必须不满足b<=a<=c)
比如这样:
mysql> select * from student_score where score >= '78';
+-----------+--------------------+-------+
| number | subject | score |
+-----------+--------------------+-------+
| 220101101 | 计算机是怎样运行的 | 78 |
| 220101102 | 计算机是怎样运行的 | 79 |
| 220101103 | 计算机是怎样运行的 | 78 |
+-----------+--------------------+-------+
3 rows in set (0.00 sec)
匹配列表中的元素:
在日常中,经常会有指定某一列的值是否在某个列表或者说不再某个列表中的搜索条件,in和not in运算符可以解决这个问题。
mysql> select * from student_info where major in ('jike');
+-----------+------+------+-----------+---------------------+-------+-----------------+
| number | name | sex | id_number | department | major | enrollment_time |
+-----------+------+------+-----------+---------------------+-------+-----------------+
| 220101101 | 一花 | 女 | 123456789 | school of computing | jike | 2022-09-01 |
| 220101102 | 二乃 | 女 | 123456788 | school of computing | jike | 2022-09-01 |
| 220101103 | 三玖 | 女 | 123456787 | school of computing | jike | 2022-09-01 |
| 220101104 | 四叶 | 女 | 123456786 | school of computing | jike | 2022-09-01 |
| 220101105 | 五月 | 女 | 123456785 | school of computing | jike | 2022-09-01 |
+-----------+------+------+-----------+---------------------+-------+-----------------+
5 rows in set (0.00 sec)
匹配NULL:
上面我们指定某一列是不是有什么可以用=,但一旦遇到NULL,就不能单纯的使用=了,而是要用专门判断是否是NULL的运算符。比如这样:
mysql> select * from student_info where major is not NULL;
+-----------+------+------+-----------+---------------------+-------+-----------------+
| number | name | sex | id_number | department | major | enrollment_time |
+-----------+------+------+-----------+---------------------+-------+-----------------+
| 220101101 | 一花 | 女 | 123456789 | school of computing | jike | 2022-09-01 |
| 220101102 | 二乃 | 女 | 123456788 | school of computing | jike | 2022-09-01 |
| 220101103 | 三玖 | 女 | 123456787 | school of computing | jike | 2022-09-01 |
| 220101104 | 四叶 | 女 | 123456786 | school of computing | jike | 2022-09-01 |
| 220101105 | 五月 | 女 | 123456785 | school of computing | jike | 2022-09-01 |
+-----------+------+------+-----------+---------------------+-------+-----------------+
5 rows in set (0.00 sec)
不能用普通的比较运算符来与NULL值进行比较,必须使用is NULL或者is not NULL。
当要多个运算符共同使用的时候就要用and和or来进行连接。
注意and 的优先级是大于or的所以要注意语句的语义和语法是否匹配
通配符:
%代表若干个字符,_代表一个字符,比如我们要查一个人的信息,但我们已经不太记得那个名字具体是什么,我们只记得很模糊的信息,用通配符就能查出来。
比如要查:
mysql> select * from student_info where name like '%三%';
+-----------+------+------+-----------+---------------------+-------+-----------------+
| number | name | sex | id_number | department | major | enrollment_time |
+-----------+------+------+-----------+---------------------+-------+-----------------+
| 220101103 | 三玖 | 女 | 123456787 | school of computing | jike | 2022-09-01 |
+-----------+------+------+-----------+---------------------+-------+-----------------+
1 row in set (0.00 sec)
但如果要查的信息里本来就有%和_, 那么我们就要这样来表示他们'/%' '/_'