08 PHP MYSQL Complex Queries Functions

Post on 11-Apr-2015

846 views 2 download

description

Slide on using PHP and MYSQL queries.

Transcript of 08 PHP MYSQL Complex Queries Functions

GeshanManandhar.comGeshanManandhar.com 11

PHP Day 08PHP Day 08

Geshan ManandharGeshan ManandharDeveloper,Developer,

Young Innovations Private LimitedYoung Innovations Private Limitedwww.geshanmanandhar.com/slides/www.geshanmanandhar.com/slides/phpphp

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

GeshanManandhar.com 2

MYSQL queriesMYSQL queries

►MYSQL query with sorting ASC and MYSQL query with sorting ASC and DESCDESC

►MYSQL query with limit and offsetMYSQL query with limit and offset►MYSQL query with join 2 tablesMYSQL query with join 2 tables►MYSQL query with count, sum, avg, MYSQL query with count, sum, avg,

minmin►MYSQL query with string concatMYSQL query with string concat►MYSQL query with like % and _MYSQL query with like % and _

GeshanManandhar.com 3

tbl_user_test schematbl_user_test schema

GeshanManandhar.com 4

tbl_test_user instance/datatbl_test_user instance/data

GeshanManandhar.com 5

Sorting With MYSQLSorting With MYSQL

►SELECT tu.user_id, tu.user_login, SELECT tu.user_id, tu.user_login, tu.emailtu.emailFROM tbl_user_test AS tuFROM tbl_user_test AS tuORDER BYORDER BY tu.user_login tu.user_login ASCASC;;

►SELECT tu.user_id, tu.user_login, SELECT tu.user_id, tu.user_login, tu.email FROM tbl_user_test as tu tu.email FROM tbl_user_test as tu ORDER BYORDER BY tu.email tu.email DESCDESC;;

►ASC – Ascending taken by default.ASC – Ascending taken by default.

GeshanManandhar.com 6

Limit in MYSQLLimit in MYSQL

►SELECT * FROM `tbl_user_test` SELECT * FROM `tbl_user_test` LIMIT LIMIT 11; - select only one row.; - select only one row.

►SELECT * FROM `tbl_user_test` SELECT * FROM `tbl_user_test` LIMIT LIMIT 1,31,3; - Select 2; - Select 2ndnd row and 3 rows row and 3 rows including 2including 2ndnd row. row. Good to use for paginationGood to use for pagination

GeshanManandhar.com 7

Table joins with MYSQLTable joins with MYSQL

► SELECT tu.user_login, tl.log_textSELECT tu.user_login, tl.log_textFROM tbl_user_test AS tu, tbl_log AS tlFROM tbl_user_test AS tu, tbl_log AS tlWHERE tu.user_id = tl.tbl_user_test_user_id;WHERE tu.user_id = tl.tbl_user_test_user_id;

GeshanManandhar.com 8

Calculations with MYSQLCalculations with MYSQL

►SELECT MIN( tp.price ) SELECT MIN( tp.price ) FROM tbl_products AS tp; - Applicable FROM tbl_products AS tp; - Applicable of single columnof single column

►SELECT tp.type, MIN( tp.price ) SELECT tp.type, MIN( tp.price ) FROM tbl_products AS tpFROM tbl_products AS tpGROUP BY tp.type; - simple group by.GROUP BY tp.type; - simple group by.

►SELECT COUNT( * ) SELECT COUNT( * ) FROM tbl_products; - single columnFROM tbl_products; - single column

GeshanManandhar.com 9

► SELECT tp.type, COUNT( tp.name ) SELECT tp.type, COUNT( tp.name ) FROM tbl_products AS tpFROM tbl_products AS tpGROUP BY tp.type; GROUP BY tp.type;

GeshanManandhar.com 10

Sum and AverageSum and Average

►SELECT SELECT SUMSUM( tp.price ) ( tp.price ) FROM tbl_products AS tp;FROM tbl_products AS tp;

►SELECT tp.type, SELECT tp.type, SUMSUM(tp.price) FROM (tp.price) FROM tbl_products AS tp GROUP BY tp.type;tbl_products AS tp GROUP BY tp.type;

►SELECT SELECT AVGAVG( tp.price ) ( tp.price ) FROM tbl_products AS tp; FROM tbl_products AS tp;

►SELECT tp.type, SELECT tp.type, AVGAVG(tp.price) FROM (tp.price) FROM tbl_products AS tp GROUP BY tp.type;tbl_products AS tp GROUP BY tp.type;

GeshanManandhar.com 11

String ConcatString Concat

► SELECT CONCAT(tc.first_name, ' ', SELECT CONCAT(tc.first_name, ' ', tc.middle_name, ' ', tc.last_name) AS tc.middle_name, ' ', tc.last_name) AS FullName FROM tbl_client AS tc;FullName FROM tbl_client AS tc;

GeshanManandhar.com 12

CONCAT_WSCONCAT_WS

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

GeshanManandhar.com 13

Select for searchesSelect for searches

► SELECT * SELECT * FROM `tbl_user_test` FROM `tbl_user_test` WHERE user_login LIKE 'user%' ;WHERE user_login LIKE 'user%' ;

►%: Matches any number of characters, even %: Matches any number of characters, even zero characters.zero characters.

GeshanManandhar.com 14

Select for searchesSelect for searches

► SELECT * SELECT * FROM `tbl_user_test` FROM `tbl_user_test` WHERE user_login LIKE 'user_' ;WHERE user_login LIKE 'user_' ;

► _: Matches exactly one character._: Matches exactly one character.

GeshanManandhar.com 15

QuestionsQuestions

GeshanManandhar.com 16

Lets run some SQL queriesLets run some SQL queries

►Show users in ascending and Show users in ascending and descending order as per user_login.descending order as per user_login.

►Show users of row 2-5 in ascending Show users of row 2-5 in ascending order as per email.order as per email.

►Show maximum price from Show maximum price from tbl_products grouped by type.tbl_products grouped by type.

►Experiment concat and search Experiment concat and search queries…queries…