#Mysql变量的定义与应用
#一、标识符命名规范
#1、不能以数字开头
#2、不能使用关键字
#3、只能使用_和$符号,不允许使用其他符号
#定义MySQL变量的方法
set @userName="小可爱";
#读取变量
SELECT @userName as '名称';
#读取时包含赋值操作
SELECT @userName:='海绵宝宝1' as '赋值查询名称';
#定义数值类型,看是否可以进行四则运算
#数值类型的四则运算
set @x = "5",@y="7";#定义多个变量时可以使用,作为分隔
SELECT @x + @y as '加法运算',@x - @y as '减法运算',
@x * @y as '乘法运算',@x/ @y as '除法运算',@x % @y as '取模操作';#除法保留4位有效小数
#关系运算
SELECT @x < @y as '大小判断';#返回结果1代表true,如果是0代表false
SELECT @x >= @y;
#逻辑运算
SELECT TRUE or FALSE;#依然符合&(and)与、|(or)或。
#通过变量进行实际的操作
set @cityName = 'Rotterdam';
SELECT * from city where `Name` = @cityName;
set @cityName1 = 'Rotterdam';
set @cityName2 = 'Zaanstad';
set @cityName3 = 'Zwolle';
SELECT * from city where `Name` in(@cityName1,@cityName2,@cityName3);
#浮点数0的处理
set @dx = 0.01,@dy = 55.5;
SELECT @dx + @dy;显示结果有很多的0;
set @result = @dx+@dy;
SELECT @result;#去掉0