Like this presentation? Why not share!

# Mysql:Operators

## on Jan 08, 2010

• 3,223 views

Mysql:Operators

Mysql:Operators

### Views

Total Views
3,223
Views on SlideShare
3,164
Embed Views
59

Likes
0
0
0

### 2 Embeds59

 http://www.ustudy.in 53 http://ustudy.in 6

### Report content

• Comment goes here.
Are you sure you want to

## Mysql:OperatorsPresentation Transcript

• MYSQL OPERATORS
• OverviewPrecedence of operators and type conversionArithmetic operatorsBitwise operatorsLogical operatorsComparison operatorsMathematical functionsString functionsDate functions
• Precedence of operators
1.Interval
2.binary,collate
3.!
4.-(unary minus),~(unary bit inversion)
5.^
6.*,/, div, %, mod
7.+,-
• 8.&lt;&lt;, &gt;&gt;
9.&
10.|
11. =, &lt;=&gt;, &gt;=, &gt;, &lt;=, &lt;, &lt;&gt;, !=, IS, LIKE, REGEXP, IN
12.between,case,when,then,else
13.Not
14.&&,and
15.Xor
16.||, or
17. :=
• Ex:
Mysql&gt;select 3-2%1;
3
Mysql&gt;select 4+5-1/2*2;
8.0000
Mysql&gt;select 23+13&&13;
1
• ARITHMATIC OPERATORS
• All arithmetic operations are calculated with BIGINT(64-bit) precision.
Ex:
Mysql&gt;select 4+55;
59
Mysql&gt;select 4-55;
-51;
Mysql&gt;select -5;
-5 (unary minus)
• Mysql&gt;select 122%45;
32
Mysql&gt;select 100/0;
NULL
Mysql&gt;select 10/3;
3.3333
Mysql&gt;select 10 div 3;
3 (integer division)
• BITWISE OPERATORS
• All the operands should be numerals, but they are internally represented as binary.
Left shift(&lt;&lt;)
Mysql&gt;select 4&lt;&lt;2;
16
Right shift(&gt;&gt;)
Mysql&gt;select 4&gt;&gt;2;
1
• Bitwise OR(|)
Mysql&gt;select 23|12;
31
Bitwise AND(&)
Mysql&gt;select 23&12;
4
Bitwise XOR(^)
Mysql&gt;select 1^1;
0
• Mysql&gt;select 0^1;
1
Mysql&gt;select 0^0;
0
Bitwise NOT
Mysql&gt;select 5&~1;
4
Mysql&gt;select 6&~6;
0
• LOGICAL OPERATORS
• Ex:
Mysql&gt;create table employee(
Ssn INT NOT NULL,
Salary INT,
Name VARCHAR(20),
Department VARCHAR(20),
Primary key(ssn));
Mysql&gt;insert into employee
Values(100,10000,’sneha’,’finance’);
• Mysql&gt;insert into employee
Values(200,20000,’shalini’,’hr’);
Mysql&gt;insert into employee
Values(300,30000,’john’,’finance’);
Mysql&gt;insert into employee
Values(400,40000,’jack’,’hr’);
• Mysql&gt;select * from employee;
• Logical AND
Mysql&gt;select ssn, name from employee where salary&gt;10000 && salary&lt;40000;

• Logical OR
mysql&gt;select name, salary, ssn from employee where
Name like ‘%s’ || department like ‘%f’;

• Logical NOT
mysql&gt;select name, department from employee where ssn!=100;

• Comparison operatorsThese operators return 1 if true else 0 if the condition is false
• Returns 1 if the condition is TRUE and returns 0 if the condition is FALSE
Mysql&gt;select 2.34&lt;=2.34;
1
Mysql&gt;select 2.34&lt;=1.24;
0
Mysql&gt;select 4.2&gt;=4;
1
• Mysql&gt;select 10!=10;
o
Mysql&gt;select 5!=10;
1
Mysql&gt; select 3 between 1 and 4;
1
Mysql&gt;select greatest(10,20,99);
99
• MATHEMATICAL FUNCTIONS
• Mysql&gt;select cos(89);
0.510177044941669
Mysql&gt;select atan(90);
1.55968567289729
Mysql&gt;select floor(1.34);
1
Mysql&gt;select ceil(1.34);
2
Mysql&gt;select format(234567.34356,3)
234,567.344
• Mysql&gt;select pi();
3.141593
1.5707963267949
Mysql&gt;select degrees(1.57);
89.9543738355392
Mysql&gt;select truncate(4.34,1);
4.3
Mysql&gt;select truncate(23.22,-1);
20
• STRING FUNTIONS
• Mysql&gt;select concat(“winners”, ”attitude”);
winnersattitude
Mysql&gt;select concat_ws(‘\$’,’pope’,’ john’, ’paul’);
pope\$john\$paul
(concats the strings by placing the separator in between the strings)
Mysql&gt;select char_length(‘mysql’);
5
• Mysql&gt;select lcase(‘OPERATORS’);
operators
Mysql&gt;select length(‘personalexcellence’);
18
Mysql&gt;select locate(‘point’, ’powerpoint’);
6
(returns the position of the substring in the mainstring)
• Mysql&gt;select repeat(‘win’,3);
winwinwin
Mysql&gt;select replace(‘myindia’, ’my’, ’our’);
ourindia
Mysql&gt;select reverse(‘apple’);
elppa
• Mysql&gt;select substring(‘mysql’,3);
sql
(returns the substring at the given position from the main string)
Mysql&gt;select format(233444.564678,3);
233,444.565
(formats the given string and rounds to the given digits after the decimal point)
• DATE FUNCTIONS
• Ex:
Mysql&gt;select curdate();
2009-12-29
Mysql&gt;select curtime();
13:04:21
Mysql&gt;select now();
2009-12-29 13:05:12
• Mysql&gt;select monthname(‘2009-10-09’);
october
Mysql&gt;select month(‘2009-10-09’);
10 (returns the month in numeric format)
Mysql&gt;select minute(‘13:05:12’);
5
Mysql&gt;select hour(‘13:05:12’);
13
• Mysql&gt;select dayname(‘2010-01-01’);
Friday
Mysql&gt;select dayofmonth(‘2010-01-01’);
1 (returns the day in the range 1 to 31)
Mysql&gt;select dayofweek(‘2010-21-01’):
5 (returns the day in the range 1 to 7)
• Mysql&gt;select datediff(‘2009-1-1’,’2009-10-10’);
83
(returns the difference between the dates in days)