版权声明:本文为博主原创学习文章,可随意转载,欢迎大家留言评论。 https://blog.csdn.net/Zhang_Yixuan_ss/article/details/86077111
Linux下的MySQL基础学习一:https://blog.csdn.net/Zhang_Yixuan_ss/article/details/85953933
一、select语句详解
SELECT 要查询的列名 FROM 表名字 WHERE 限制条件
where限制条件,用于达到更加精确的查询,可以有数学符号 (=,<,>,>=,<=
),例如:
select name,age,phone from employee where name = 'Mary';
或者
select name,age from employee where age>25;
where后面可以有不止一条限制,而根据条件之间的逻辑关系,可以用 [条件一 OR 条件二]
] 和 [条件一 AND 条件二
] 连接.例如:
select name,age from employee where age<25 or age>30;
select name,age from employee where age>25 and age<30;
或者
select name,age from employee where age between 25 and 30;
关键词 IN 和 NOT IN
和它们的名字一样明显,用于筛选“在”或“不在”某个范围内的结果,比如说我们要查询在 dpt3 或 dpt4 的人:
SELECT name,age,phone,in_dpt FROM employee WHERE in_dpt IN ('dpt3','dpt4');
SELECT name,age,phone,in_dpt FROM employee WHERE in_dpt NOT IN ('dpt1','dpt3');
关键字 LIKE
可用于实现模糊查询,常见于搜索功能中。
和 LIKE 联用的通常还有通配符,代表未知字符。SQL中的通配符是 _
和 %
。其中 _
代表一个未指定字符,%
代表不定个未指定字符
比如,要只记得电话号码前四位数为1101,而后两位忘记了,则可以用两个 _
通配符代替;另一种情况,比如只记名字的首字母,又不知道名字长度,则用 %
通配符代替不定个字符。
SELECT name,age,phone FROM employee WHERE phone LIKE '1101__';
SELECT name,age,phone FROM employee WHERE name LIKE 'J%';