Operators for MySQL Learning

Write directory title here

operator

1. Arithmetic operators + - * /(div) %(mod)

Arithmetic operators are mainly used for mathematical operations, which can connect two values ​​​​or expressions before and after the operator, and perform addition
(+), subtraction (-), multiplication (*), division (/) and extraction of values ​​or expressions Modulo (%) operation.

1.1 Addition and subtraction operators

SELECT 100, 100 + 0, 100 - 0, 100 + 50, 100 + 50 -30, 100 + 35.5, 100 - 35.5
FROM DUAL;

# 在sql中 +没有连接的作用,就表示加法运算,此时会将字符串转换为数值(隐式转换)
SELECT 100+'1'   #在Java中,结果为1001
FROM DUAL;     # 101

SELECT 100+'a'
FROM DUAL;    # 'a'看作0处理

SELECT 100+NULL
FROM DUAL;    # NULL参与运算结果为NULL

1.2 Multiplication and division operators

In the division, the results of division and non-division are all floating-point type (reserve four digits after the decimal point). Multiply and divide first, then add and subtract. The
denominator in div cannot be 0, and the result is NULL

SELECT 100,100*1,100*1.0,100/1.0,100/2,
100+2*5/2,100/3,100 DIV 0   # 一个数乘以整数1和除以整数1后仍得原数
FROM DUAL;					# 除法中分母为0,结果则为NULL

# 取模
# 结果的正负取决于被模数的符号 -12 MOD -5
SELECT 12%3,12%5,12 MOD -5,-12 MOD 5,-12 MOD -5
FROM DUAL;

# 1.21 查询员工id为偶数的员工信息
SELECT *
FROM employees
WHERE employee_id%2=0;

2. Comparison operators

The comparison operator is used to compare the operand on the left side of the expression with the operand on the right side. If the result of the comparison is true, it returns 1, if the result of the comparison is
false, it returns 0, and otherwise it returns NULL.
Comparison operators are often used as the conditions of the SELECT query statement to return the result records that meet the conditions.

Comparison operators: = <=> (safe equal to) <>(!=) (not equal to) < <= > >=

2.1 Use of = (equal sign)

SELECT 1=2 ,1!=2,1='1',1='a',0='a',100='100abncx'
FROM DUAL;    #字符串作比较时,如果不能转化为数值的话,一般隐式转换为0参与运算(100='100abncx')

SELECT 'a'='a','ab'='ab','a'='b' 
FROM DUAL;    #等号两边的值、字符串或表达式都为字符串时,直接比较时,对比字符的ANSI码数值

SELECT 1=NULL,NULL=NULL
FROM DUAL;    #如果等号两边的值、字符串或表达式中有一个为NULL,则比较结果为NULL

SELECT employee_id,salary,commission_pct
FROM employees
WHERE employee_id=120;
#WHERE commission_pct =NULL;
# NULL和NULL相比较 结果都为NULL,查询出的都是返回结果为1的结果,为1的显示,其他的过滤掉

2.2 <=> (safe equal operator)

# 安全等于可以对NULL进行判断,在两个操作数都为NULL时其返回值为1,而不为NULL
# 当一个操作数为NULL时,其返回值为0,而不为NULl
# <=> 为NULL而生,
SELECT NULL<=>NULL,1=NULL,1<=>'1',1<=>'a',0<=>'a'
FROM DUAL;

#查询commission_pct为NULL的数据
SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct<=>NULL;

2.3 <> (not equal to operator) (!=)

# 用于判断两边的数字、字符串或者表达式的值是否不相等
# 如果不相等则返回1,相等则返回0  不等于运算符不能判断NULl值
# 两边任意有一个NULL值,则结果为NULL

SELECT 3<>2,'4'<>NULL,''<>NULL,NULL!=NULL
FROM DUAL;

2.4 Comparison keywords (non-symbolic operators)

IS NULL (for null operator) IS NOT NULL (not for null operator)
ISNULL lEAST (minimum value operator) GREATEST (maximum value operator)

2.41.IS NULL (for the null operator)

# IS NULL
# 查询commission_pct为NULL的数据
SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct IS NULL;

2.42. ISNULL(field)

# ISNULL(字段)
# 查询commission_pct为NULL的数据
SELECT last_name,salary,commission_pct
FROM employees
WHERE ISNULL(commission_pct) ;

2.43.IS NOT NULL (not null operator)

# IS NOTNULL
# 查询commission_pct不为NULL的数据
SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;

SELECT last_name,salary,commission_pct
FROM employees
WHERE NOT commission_pct <=>NULL;  #NOT 假变真,真变假 1->0  0->1

2.44.lEAST (minimum value operator) GREATEST (maximum value operator)

# LEAST 最小值运算符
SELECT LEAST('a','h','z'),GREATEST('a','c','z')
FROM DUAL;

#比较employees中名和姓的大小
SELECT LEAST(first_name,last_name),LEAST(LENGTH(first_name),LENGTH(last_name))
FROM employees;

2.45. BETWEEN a AND b

# BETWEEN  a AND b  查询[a,b]范围内的数据 ,a<b
# 查询工资在6000-8000的员工信息
SELECT employee_id,last_name,salary
FROM employees
WHERE salary BETWEEN 6000 AND 8000;
#WHERE salary>=6000 AND salary<=8000;   WHERE salary>=6000 && salary<=8000;

#查询工资不在6000-8000的员工信息
SELECT employee_id,last_name,salary
FROM employees
#where salary not between 6000 and 8000;
WHERE salary<6000 OR salary >8000;

2.46.in(set) / not in(set)

#查询部门为10,20,30的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE department_id IN(10,20,30);
#where department_id =10 or department_id=20 or department_id=30;

#查询工资不是6000,7000,8000的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE salary NOT IN(6000,7000,8000);

2.47. Fuzzy query LIKE

# %:代表不确定个数的字符(0个,1个或者多个)

#查询last_name 中包含字符'a'的员工的信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE last_name LIKE '%a%';

#查询last_name 中以字符'a'开头的员工的信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE last_name LIKE 'a%'; 
WHERE last_name LIKE '%a';  # 以字符'a'结尾的员工的信息

#查询last_name 中包含字符'a'且包含字符'e'的员工信息
#写法一:
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';

#写法二:
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE last_name LIKE '%a%e%' OR last_name LIKE '%e%a%';   #a在前面或者e在前面

# _代表一个不确定的字符

#查询第二个字符是'a'的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees 
WHERE last_name LIKE '_a%';

#查询第二个字符是_且第三个字符是'a'的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
# WHERE last_name LIKE '_\_a%';   #使用转义字符 \ 第二个字符是_,第三个是字符'a'
#或者 使用ESCAPE 指定转义字符
WHERE last_name LIKE '_$_a%' ESCAPE '$'; 

2.48. REGEXP \RLIKE regular expression

# ^ 匹配文本的开始字符   $ 匹配文本结束的字符  
SELECT 'cxwstart' REGEXP '^c', 'cxwstart' REGEXP 't$','cxwstart' REGEXP 'xw'
FROM DUAL; 

# . 'b.t' 匹配b到t之间有一个字符的字符串  big bit but
#有两个就是 .. 
SELECT 'cxwlearning' REGEXP 'cxwlearni.g','cxwlearning' REGEXP 'cxwlearn..g'
FROM DUAL;           #cxwlearni.g 匹配cxwlearni到g之间有一个字符的字符串

# '[ab]' 字符包含a或者b的字符串
SELECT 'cxwacd' REGEXP '[ab]'
FROM DUAL;

3. Logical operators

  1. Logical operators are mainly used to judge the truth or falsehood of an expression
  2. The return value is 0, 1 or NULL
  3. OR(||) AND(&&) NOT(!) XOR(logic exclusive or)

3.1.OR(||) AND(&&)

# OR(||) AND(&&)  AND的优先级高于OR ,先对AND两边的操作数进行操作再进行OR
#查询部门是10,20的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
#where department_id =10 or department_id=20;
WHERE department_id =50 AND salary >6000;

NOT(!)

# NOT(!)
#查询工资不在6000-8000的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE salary NOT BETWEEN 6000 AND 8000;

#查询commission_pct 不为空的员工信息
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE commission_pct IS NOT NULL; 

XOR (logic exclusive or)

# XOR 表达式两边 只满足两边其中的一个条件
SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE department_id =50 XOR salary >6000;

4. Bitwise operators

Bitwise operators are operators that perform calculations on binary numbers. The bit operator will first convert the operand into a binary number, then perform a bit operation, and
finally convert the calculation result from binary to

decimal. << (move left) >> (move right)

4.1.&(and) |(or) ^(exclusive or) ~(reverse)

# 转换为二进制
# & 有假则假,全真为真
# | 有真则真,全假为假
# ^ 不同才为1

SELECT 12&5 ,12|5,12^5
FROM DUAL;

# ~按位取反 1->二进制 00000001 ~1->二进制 11111110
SELECT 10 & ~1
FROM DUAL;

4.2.<<(left shift) >>(right shift)

# << >>
#在一定范围内满足,每左移一位,相当于乘以2,没向右移一位,相当于除以2
SELECT 4<<1 ,8>>1
FROM DUAL;

practise

1. Select the name and salary of employees whose salary is not between 5000 and 12000

SELECT employee_id,last_name,salary
FROM employees
#where salary not between 5000 and 12000;
WHERE salary <5000 OR salary >12000;

2. Select the name and department number of the employee who works in department number 20 or number 50

SELECT employee_id,last_name,department_id
FROM employees
#WHERE department_id in(20,50);
WHERE department_id =20 OR department_id=50;

3. Select the name and department number of the employee without a manager in the company

FROM employees
#where manager_id is null;
#WHERE ISNULL(manager_id);
WHERE manager_id <=>NULL;

4. Select the name, salary and bonus level of the employees with bonuses in the company

SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;

SELECT last_name,salary,commission_pct
FROM employees
WHERE NOT commission_pct <=>NULL;

5. Select the employee name whose third letter is a fuzzy query

SELECT employee_id, last_name,salary
FROM employees
WHERE last_name LIKE'__a%';

6. Select employee names with letters a and k in their name

SELECT employee_id,last_name,salary
FROM employees
WHERE last_name LIKE '%a%k%' OR last_name LIKE '%k%a%';

SELECT employee_id,last_name,salary
FROM employees
WHERE last_name LIKE '%a%' AND last_name LIKE '%k%';

7. Display the employee information ending with 'e' in first_name in the employees table in the table

SELECT employee_id,first_name,salary
FROM employees
WHERE first_name REGEXP 'e$';  #以'e'开头的员工信息 REGEXP '^e';

SELECT employee_id,first_name,salary
FROM employees
WHERE first_name LIKE '%e';  

8. Display the name and type of work of the department number between 80 and 100 in the table employees

SELECT employee_id,last_name,salary,department_id
FROM employees
WHERE department_id BETWEEN 80 AND 100;

9. Show that the manager_id of the table employees is 100, 101, 110 employee name, salary, manager id

SELECT last_name,salary,manager_id
FROM employees
WHERE manager_id IN(100,101,110);

Guess you like

Origin blog.csdn.net/cyaya6/article/details/126043895