mysql中有4類運算符,它們是:
算術操作符是SQL中最基本的操作運算符,主要有一下幾種運算符:
+(加)、 -(減)、 *(乘)、 /(除)、 %(求余或者模)
算術操作符測試案例:
創建表tmp14,定義數據類型為INT的字段num,插入值64,對num值進行算術運算:
首先創建表tmp14,輸入語句如下:
CREATE TABLE tmp14 ( num INT);
向字段num插入數據64:
INSERT INTO tmp14 value(64);
接下來,對num值進行加法和減法運算:
SELECT num, num+10, num-3+5, num+5-3, num+36.5 FROM tmp14;
SELECT num, num *2, num /2, num/3, num%3 FROM tmp14;
SELECT 1=0, '2'=2, 2=2,'0.02'=0, 'b'='b', (1+3) = (2+2),NULL=NULL;
SELECT 'good'<>'god', 1<>2, 4!=4, 5.5!=5, (1+3)!=(2+1),NULL<>NULL;
SELECT 'good'<='god', 1<=2, 4<=4, 5.5<=5, (1+3) <= (2+1),NULL<=NULL;
SELECT 'good'<'god', 1<2, 4<4, 5.5<5, (1+3) < (2+1),NULL<NULL;
SELECT 'good'>='god', 1>=2, 4>=4, 5.5>=5, (1+3) >= (2+1),NULL>=NULL;
SELECT 'good'>'god', 1>2, 4>4, 5.5>5, (1+3) > (2+1),NULL>NULL;
SELECT NULL IS NULL, ISNULL(NULL),ISNULL(10), 10 IS NOT NULL;
SELECT 4 BETWEEN 4 AND 6, 4 BETWEEN 4 AND 6,12 BETWEEN 9 AND 10;
SELECT 'x' BETWEEN 'f' AND 'g', 'b' BETWEEN 'a' AND 'c';
SELECT least(2,0), least(20.0,3.0,100.5), least('a','c','b'),least(10,NULL);
SELECT greatest(2,0), greatest(20.0,3.0,100.5), greatest('a','c','b'),greatest(10,NULL);
SELECT 2 IN (1,3,5,'thks'), 'thks' IN (1,3,5,'thks');
SELECT NULL IN (1,3,5,'thks'),10 IN (1,3,NULL,'thks');
SELECT 'stud' LIKE 'stud', 'stud' LIKE 'stu_','stud' LIKE '%d','stud' LIKE 't_ _ _', 's' LIKE NULL;
SELECT 'ssky' REGEXP '^s', 'ssky' REGEXP 'y$', 'ssky' REGEXP '.sky', 'ssky' REGEXP '[ab]';
SELECT NOT 10, NOT (1-1), NOT -5, NOT NULL, NOT 1 + 1;
SELECT !10, !(1-1), !-5, ! NULL, ! 1 + 1;
SELECT 1 AND -1,1 AND 0,1 AND NULL, 0 AND NULL;
SELECT 1 && -1,1 && 0,1 && NULL, 0 && NULL;
SELECT 1 OR -1 OR 0, 1 OR 2,1 OR NULL, 0 OR NULL, NULL OR NULL;
SELECT 1 || -1 || 0, 1 || 2,1 || NULL, 0 || NULL, NULL || NULL;
SELECT 1 XOR 1, 0 XOR 0, 1 XOR 0, 1 XOR NULL, 1 XOR 1 XOR 1;
執行上面的語句,結果如下。
SELECT 10 | 15, 9 | 4 | 2;
SELECT 10 & 15, 9 &4& 2;
SELECT 10 ^ 15, 1 ^0, 1 ^ 1;
SELECT 1<<2, 4<<2;
SELECT 1>>1, 16>>2;
SELECT 5 & ~1;
運算符的優先級