100% found this document useful (6 votes)
1K views16 pages

08 PHP MYSQL Complex Queries Functions

This document discusses various MySQL queries including: 1) Sorting data in ascending and descending order using ORDER BY. 2) Using LIMIT to select a specified number of rows or rows within a specified range for pagination. 3) Joining data from two tables using a common column. 4) Performing calculations like COUNT, SUM, AVG, MIN on data and optionally grouping the results. 5) Concatenating strings using functions like CONCAT and CONCAT_WS. 6) Searching for strings using wildcards like % and _ with the LIKE operator. The document provides examples of each type of query using sample tables and encourages experimenting with different queries.

Uploaded by

Geshan Manandhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd
100% found this document useful (6 votes)
1K views16 pages

08 PHP MYSQL Complex Queries Functions

This document discusses various MySQL queries including: 1) Sorting data in ascending and descending order using ORDER BY. 2) Using LIMIT to select a specified number of rows or rows within a specified range for pagination. 3) Joining data from two tables using a common column. 4) Performing calculations like COUNT, SUM, AVG, MIN on data and optionally grouping the results. 5) Concatenating strings using functions like CONCAT and CONCAT_WS. 6) Searching for strings using wildcards like % and _ with the LIKE operator. The document provides examples of each type of query using sample tables and encourages experimenting with different queries.

Uploaded by

Geshan Manandhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd
You are on page 1/ 16

PHP Day 08

http://www.php.net
http://www.mysql.com

Geshan Manandhar
Developer,
Young Innovations Private Limited
www.geshanmanandhar.com/slides/php
GeshanManandhar.com 1
MYSQL queries
► MYSQL query with sorting ASC and
DESC
► MYSQL query with limit and offset
► MYSQL query with join 2 tables
► MYSQL query with count, sum, avg,
min
► MYSQL query with string concat
► MYSQL query with like % and _
GeshanManandhar.com 2
tbl_user_test schema

GeshanManandhar.com 3
tbl_test_user instance/data

GeshanManandhar.com 4
Sorting With MYSQL
► SELECT tu.user_id, tu.user_login,
tu.email
FROM tbl_user_test AS tu
ORDER BY tu.user_login ASC;
► SELECT tu.user_id, tu.user_login,
tu.email FROM tbl_user_test as tu
ORDER BY tu.email DESC;
► ASC – Ascending taken by default.

GeshanManandhar.com 5
Limit in MYSQL
► SELECT * FROM `tbl_user_test` LIMIT
1; - select only one row.
► SELECT * FROM `tbl_user_test` LIMIT
1,3; - Select 2nd row and 3 rows
including 2nd row.
 Good to use for pagination

GeshanManandhar.com 6
Table joins with MYSQL
► SELECTtu.user_login, tl.log_text
FROM tbl_user_test AS tu, tbl_log AS tl
WHERE tu.user_id = tl.tbl_user_test_user_id;

GeshanManandhar.com 7
Calculations with MYSQL
► SELECT MIN( tp.price )
FROM tbl_products AS tp; - Applicable
of single column
► SELECT tp.type, MIN( tp.price )
FROM tbl_products AS tp
GROUP BY tp.type; - simple group by.
► SELECT COUNT( * )
FROM tbl_products; - single column
GeshanManandhar.com 8
► SELECTtp.type, COUNT( tp.name )
FROM tbl_products AS tp
GROUP BY tp.type;

GeshanManandhar.com 9
Sum and Average
► SELECT SUM( tp.price )
FROM tbl_products AS tp;
► SELECT tp.type, SUM(tp.price) FROM
tbl_products AS tp GROUP BY tp.type;
► SELECT AVG( tp.price )
FROM tbl_products AS tp;
► SELECT tp.type, AVG(tp.price) FROM
tbl_products AS tp GROUP BY tp.type;
GeshanManandhar.com 10
String Concat
► SELECT CONCAT(tc.first_name, ' ',
tc.middle_name, ' ', tc.last_name) AS
FullName FROM tbl_client AS tc;

GeshanManandhar.com 11
CONCAT_WS
► SELECT CONCAT_WS(' ', tc.first_name,
tc.middle_name, tc.last_name) AS FullName
FROM tbl_client AS tc;

GeshanManandhar.com 12
Select for searches
► SELECT *
FROM `tbl_user_test`
WHERE user_login LIKE 'user%' ;
► %: Matches any number of characters, even
zero characters.

GeshanManandhar.com 13
Select for searches
► SELECT *
FROM `tbl_user_test`
WHERE user_login LIKE 'user_' ;
► _: Matches exactly one character.

GeshanManandhar.com 14
Questions

GeshanManandhar.com 15
Lets run some SQL queries
► Show users in ascending and
descending order as per user_login.
► Show users of row 2-5 in ascending
order as per email.
► Show maximum price from
tbl_products grouped by type.
► Experiment concat and search
queries…
GeshanManandhar.com 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy