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... Aggregate
Trang 1Using Basic Functions in MySQL
Session 12
Trang 2 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
Review - I
Trang 3Review - 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
Trang 4 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
Trang 5Basics 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
Trang 6Using 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
Trang 7Aggregate Functions - I
AVG function
Returns the average of the values of the argument
The syntax for obtaining the average of the
Trang 8Aggregate 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)
Trang 9Mathematical Functions using
MySQL - I
MySQL provides with mathematical functions that operate
on data and return numerical value
Returns cosine of an argument
The syntax for obtaining the cosine of an argument X is:
COS(X)
Trang 10Mathematical 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)
Trang 11Mathematical 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)
Trang 12Mathematical Functions using
MySQL - IV
LOG function
Enables a user to enter one or two arguments
The syntax for specifying one argument in a LOG
Trang 13Mathematical Functions using
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)
Trang 14Mathematical 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)
Trang 15Date 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)
Trang 16Date Functions in MySQL - II
Trang 17Date Functions in MySQL - III
Returns the name of the weekday for a date
The syntax for obtaining the name of the week day is:
DAYNAME(date)
Trang 18Date 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)
Trang 19String Functions Using MySQL - I
String Functions operate on character type of data
Trang 20String 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, )
Trang 21String 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:
Trang 22String Functions Using MySQL - IV
LENGTH function
Returns the length of a string in bytes
The syntax for obtaining the length of the string is:
Trang 23String Functions Using MySQL - V
Adds a string to a string at a specified position
The syntax for insert function is:
INSERT(str,pos,len,newstr)
Trang 24String Functions Using MySQL - VI
LOWER function
Changes the string entered into lowercase
The syntax for converting into lower case is:
LOWER(str)
Trang 25System 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)
Trang 26System Information Functions using MySQL - II
CHARSET function
Returns the character set of the argument
The syntax for obtaining the character set of a
string is:
CHARSET (str)
CONNECTION_ID function
Returns the thread ID for the connection
The syntax for obtaining the thread ID is:
CONNECTION_ID ()
Trang 27System Information Functions using MySQL - III
Returns the name of the current database
The syntax for obtaining the database name is:
DATABASE()
Trang 28System Information Functions using MySQL - IV
VERSION function
Returns version of MySQL
The syntax for obtaining the version is:
VERSION()
Trang 29Summary – I
The functions are used in two ways in the SQL queries:
In place of the column name
In the WHERE clause
The GROUP BY clause is used with the functions to group the data on a specific constraint
The mathmematical functions are used to operate on
numbers The examples of mathematical functions are COS, SIN, TAN, and several others
The mathematical fuction returns a NULL value if error occurs while processing
Trang 30Summary – II
The string function operates on character type of data
The date functions are used to perfom computations on the date type fo data These fucntions are used to add the date, subtract the date, and extract a part of the date.
The system functions are used to obtain sytem related information