sql基础语法

UNION ALL 和UNION操作符
合并两个或者多个SELECT语句的结果集
注意:必须拥有相同数量的列,列也必须有相同的数据类型

IF表达式
SELECT IF(FALSE,1+1,1+2);IFNULL表达式
判断第一个参数expr1是否为空
如果为空,则返回第二个参数expr2
如果不为空,则直接返回第一个参数SELECT IFNULL(NULL,11)
SELECT IFNULL(06,11)

IF - then -else
将 CASE后面的case_value值和每一个when子句后面的when_value进行比较
如果一旦和when后面的值一致,则返回then后面的statement_list
如果不一致,则返回else后面的值when --> THEN 可以有多个
set @crank=0;
1.引入变量时,用set语句定义,必须赋值
否则是空值
2.变量与连接有关,也就是说,一个客户单定义的变量不能被其他客户端看到或者使用,当客户端退出时,该客户端连接的所有变量将自动释放
DISTINCT (列名)
用于返回唯一不同值(去重)
日期函数
select month('2022-03-07')
select year('2022-03-07')
select day('2022-03-07')
select year('2022-03-07'), month('2022-03-07'),day('2022-03-07')
select now(),curdate(),curtime();//当前时间
查询常量值
select 100;
select 'john'
查询表达式
select 100*98;
select 100%98;
查询函数
select version();
起别名 便于理解,区分重名
select 100%98 as 结果;
select last_name as 姓,first_name as 名 from employees;
select last_name 姓,first_name 名 from employees;
select salary as "out put" from employees; 关键字及特殊符号需要加双引号
去重 distinct
select distinct department_id from employees;
+号的作用
select 100+90; 两个操作数都为数值型,则作加法运算
select '123'+90 其中一方为字符型,试图将字符型数值转换为数值型, 如果转换成功,则继续做加法运算
select 'john'+90 如果转换失败,则将字符型数值转换为0;
select null+10 只要其中一方为null,则结果为null
拼接 concat('','','','','')
#案例 : 查询员工名和姓连接成一个字段,并显示为姓名
select concat('a','b','c') as 结果;
select concat(last_name,first_name) as 姓名 from employees;
判断空 ifnull(判断列,为真显示值)
条件查询
select 查询列表 from where 条件;
按条件表达式筛选:条件运算符:> < = != <> >= <=
案例:查询工资大于12000
select * from employees
where salary>12000;
案例:查询部门编号不等于90的员工名和部门编号
select last_name,department_id from employees
where department_id<>90;
按逻辑表达式筛选逻辑运算符: && || ! and or not
&&和and:两个条件都为true,结果为true,反之为false
||或or:只要有一个条件为true,结果为true,反之为false
!或not:如果连续的条件本身为false,结果为true,反之为false
案例:查询工资在10000在20000之间的员工名、工资以及奖金
select last_name,salary,commission_pct from employees
where salary>=10000 and salary<=20000;
案例:查询部门编号不是在90刀110之间,或者工资高于15000的员工信息
select * from employees where
department_id<90 or department_id>110 or salary>15000;
select * from employees where
not(department_id>=90 and department_id<=110) or salary>15000;
模糊查询:like between and in is null is not null
is null:仅仅可以判断null 可读性高
<=>:既可以判断null,又可以判断普通的数值 可读性低
案例:查询员工名中包含字符a的员工信息
select * from employees
where last_name like '%a%';
案例:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
select last_name,salary from employees
where last_name like '__e_a%';
案例:查询员工名中第二个字符为_的员工名
select last_name from employees
where laset_name like '_\_%';
select last_name from employees where
last_name like '_$_%; escape '$'; 此处escape为转义符,表示该符号为普通字符使用
案例:查询员工编号在100到120之间的员工信息
select * from employees where
employee_id>=100 and employee_id<=120;
select * from employees where
employee_id between 100 and 120;
案例:查询员工工种编号是 it_prog、ad_vp、ad_pres中的一个员工名和工种编号
select last_name,job_id from employees where
job_id = 'it_prot' or job_id = 'ad_vp' or job_id = 'ad_pres';
select last_name,job_id from employees where
job_id in ('it_prot','ad_vp','ad_pres');
案例:查询没有奖金的员工
select last_name,commission_pct from employees where
commission_pct is null
浙公网安备 33010602011771号