Tải bản đầy đủ (.ppt) (30 trang)

slide môn học MySQL bài 6 using basic functions in MySQL

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (351.73 KB, 30 trang )

Using Basic Functions in MySQL
Session 12


Review - I






Joining of tables means combining two or more data or records
of different tables of same database into one comprehensive
structure
The field with the primary key of a table is used for joining
tables by referring it as a reference key for the next table
Advantages of joining of tables are:






Greater ease of manipulation
Increases speed in access
Reduces data redundancy

The tables are joined either with the WHERE clause used with
SELECT command or by using JOIN keyword
MySQL Database / Session 12 / Slide 2 of 30



Review - II








An equi-join can also be called as INNER JOIN. An INNER
JOIN behaves same like ‘,’ which is used for joining tables
When using INNER JOIN the common field of the joining
tables should have the matching value
An OUTER JOIN is used to join two tables, a source and
joining table, which have one or more columns in common
A self-join is a query in which a value of a table column is
joined or compared to itself

MySQL Database / Session 12 / Slide 3 of 30


Objectives








Use GROUP BY and HAVING clause in
MySQL queries
Use the Mathematical functions
Use the Date functions
Use the String functions
Use the system information function
MySQL Database / Session 12 / Slide 4 of 30


Basics of Using Functions




The functions used in MySQL are similar to any
other programming language
A function is used in two ways in a MySQL
command:
 Value to be retrieved
 Part of a WHERE clause

MySQL Database / Session 12 / Slide 5 of 30


Using GROUP BY and HAVING Clause








GROUP BY clause groups the rows with similar values
for a specific column into a single row
HAVING clause is used to define the result set based on
some set of calculations
GROUP BY clause is used with aggregate functions
The aggregate functions are AVG, COUNT, MAX, MIN,
and SUM

MySQL Database / Session 12 / Slide 6 of 30


Aggregate Functions - I




AVG function
 Returns the average of the values of the argument
 The syntax for obtaining the average of the
argument is:
AVG(expression)
COUNT function
 Returns the number of times the argument is not
NULL
 The syntax for obtaining the count is:
COUNT(expression)
MySQL Database / Session 12 / Slide 7 of 30



Aggregate Functions - II


MIN function
 Returns the minimum value of the expression
 The syntax for obtaining the smallest value is:
MIN(expression)



SUM function
 Returns the sum of the values in the expression
 The syntax for obtaining the sum is:
SUM(expression)
MySQL Database / Session 12 / Slide 8 of 30


Mathematical Functions using
MySQL - I




MySQL provides with mathematical functions that operate
on data and return numerical value
Ceiling function
 Returns the smallest integer value greater than the
argument X
 The syntax for obtain the arc tangent of an argument X is:

CEILING(X)



COS function
 Returns cosine of an argument
 The syntax for obtaining the cosine of an argument X is:
COS(X)
MySQL Database / Session 12 / Slide 9 of 30


Mathematical Functions using
MySQL - II


CRC function
 Calculates a cyclic redundancy check value
 Returns a 32 bit unsigned value
 The syntax for obtaining the cyclic redundancy
value of an argument X is:
CRC32(X)

MySQL Database / Session 12 / Slide 10 of 30


Mathematical Functions using
MySQL - III





EXP function
 Returns the value of e raised to the power of X
 The syntax for obtaining the natural logarithm of
an argument is:
EXP(X)
FLOOR function
 Returns the largest value not greater than X
 The syntax for obtaining the largest value not
greater than the argument specified is:
FLOOR(X)
MySQL Database / Session 12 / Slide 11 of 30


Mathematical Functions using
MySQL - IV


LOG function
 Enables a user to enter one or two arguments
 The syntax for specifying one argument in a LOG
function is:
LOG(X)



MOD function
 Returns the remainder of the first argument divided by the
second
 The syntax for obtaining the remainder is:

MOD(X,Y)

MySQL Database / Session 12 / Slide 12 of 30


Mathematical Functions using
MySQL - V


POWER function
 The syntax for obtaining the power is:
POWER(X,Y)

Returns the value of X raised to the power of Y
SIGN function
 Returns a -1, 0, or 1 depending on the argument is
positive, zero, or positive
 The syntax for obtaining the sign of an argument is:




SIGN(X)
MySQL Database / Session 12 / Slide 13 of 30


Mathematical Functions using
MySQL - VI



SQRT function
 Returns the non-negative square root of the argument
 The syntax for obtaining the square root of an argument is:
SQRT(X)

MySQL Database / Session 12 / Slide 14 of 30


Date Functions in MySQL - I




MySQL provides with date functions that operate on
date and time data type
ADDDATE function
 Add two date expressions
 The syntax for adding two date expressions is:
ADDDATE(expr1,days)

MySQL Database / Session 12 / Slide 15 of 30


Date Functions in MySQL - II




CURDATE function
 Returns the current date in the format YYYY-MM-DD

or YYYYMMDD format
 The syntax for obtaining the current date is:
CURDATE()
DAY function
 Returns the day of the month for the specified date.
 Range is from 1 to 31
 The syntax for obtaining the day of the month is:
DAY(date)

MySQL Database / Session 12 / Slide 16 of 30


Date Functions in MySQL - III




DATEDIFF function
 Returns the number of days between a start date and
end date
 The syntax for obtaining the date difference is:
DATEDIFF(expr1,expr2)
DAYNAME function
 Returns the name of the weekday for a date
 The syntax for obtaining the name of the week day is:
DAYNAME(date)
MySQL Database / Session 12 / Slide 17 of 30


Date Functions in MySQL - IV





MONTHNAME function
 Returns the name of the month for the date
 The syntax for obtaining the month name is:
MONTHNAME (date)
YEAR function
 Returns the year for the date
 The syntax for obtaining the year for a date argument is:
YEAR (date)
MySQL Database / Session 12 / Slide 18 of 30


String Functions Using MySQL - I



String Functions operate on character type of data
ASCII function
 Returns the numeric value of the leftmost character of the
string
 The syntax for obtaining the ASCII value is:
ASCII(string)

MySQL Database / Session 12 / Slide 19 of 30


String Functions Using MySQL - II



BIN function
 Returns a string representation of the binary value
of N, where N is the BIGINT number.
 The syntax for obtaining the string representation
is:
BIN(string)



CONCAT function
 Returns a string after concatenating the arguments
 The syntax to concatenate is:
CONCAT(STR1,STR2,...)
MySQL Database / Session 12 / Slide 20 of 30


String Functions Using MySQL - III




BIT_LENGTH function
 Returns the length of the string specified as the argument
 The syntax for obtaining the length of the string in bits
is:
BIT_LENGTH(string)
COMPRESS function
 Compresses a string

 The syntax for compressing a string is:
COMPRESS(string)
MySQL Database / Session 12 / Slide 21 of 30


String Functions Using MySQL - IV




LENGTH function
 Returns the length of a string in bytes
 The syntax for obtaining the length of the string is:
LENGTH(str)
CHAR function
 Returns a string consisting of character given by the code
value of the integers
 The syntax for obtaining the string of characters is:
CHAR(N,...)
MySQL Database / Session 12 / Slide 22 of 30


String Functions Using MySQL - V




FIELD function
 Returns the index of the string in the other strings
specified as argument

 The syntax for obtaining the string is:
FIELD(string1,string2,....)
INSERT function
 Adds a string to a string at a specified position
 The syntax for insert function is:
INSERT(str,pos,len,newstr)
MySQL Database / Session 12 / Slide 23 of 30


String Functions Using MySQL - VI


LOWER function
 Changes the string entered into lowercase
 The syntax for converting into lower case is:
LOWER(str)

MySQL Database / Session 12 / Slide 24 of 30


System Information Functions using
MySQL - I




System information functions returns the system related
information
BENCHMARK function
 Executes the expression entered as the argument

repeatedly the number of times specified in the argument
 The syntax for benchmark is:
BENCHMARK(count, expr)

MySQL Database / Session 12 / Slide 25 of 30


×