运算符的使用
1. 案例目的
创建数据表,并对表中的数据进行运算操作,掌握各种运算符的使用方法。
创建表tmp15,其中包含varchar类型的字段note和int类型的字段price,使用运算符对表tmp15中不同的字段进行运算;使用逻辑操作符对数据进行逻辑操作;使用位操作符对数据进行位操作。
创建表tmp15。
create table tmp15 (
note varchar(100),
price int
);
insert into tmp15 values(" Thisisgood",50);
2. 案例操作过程
1)对表tmp15中的整型数值字段price进行算术运算。
select price,price+10,price-10,price*2,price/2,price%3 from tmp15;
2) 对表tmp15中的整型数值字段price进行比较运算。
select price,price>10,price<10,price!=10,price=10,price<=>10,price<>10 from tmp15;
3) 判断price值是否落在30~80区间;返回与70、30相比最大的值,判断price是否为in列表(10,20,50,35)中的某个值。
select price, price between 30 and 80,greatest(price,70,30), price in(10,20,50,35) from tmp15;
4) 对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空;使用like判断是否以字母't'开头;使用regexp判断是否以字母'y'结尾;判断是否包含字母'g'或者'm'。
select note, note is null, note like 't%', note regexp '$y', note regexp '[gm]' from tmp15;
5) 将price字段值与null、0进行逻辑运算。
select price, price && 1,price && null, price || 0, price and 0, 0 and null, price or null from tmp15;
select price, !price, not null, price xor 3,0 xor null, price xor 0 from tmp15;
6) 将price字段值与2、4进行按位与、按位或操作,并对price进行按位操作。
select price, price&2, price|4,~price from tmp15;
7) 将price字段值分别左移和右移两位。
select price,price<<2,price>>2 from tmp15;