16
GeshanManandhar.com GeshanManandhar.com 1 PHP Day 08 PHP Day 08 Geshan Manandhar Geshan Manandhar Developer, Developer, Young Innovations Private Young Innovations Private Limited Limited www.geshanmanandhar.com/slides www.geshanmanandhar.com/slides /php /php http://www.php.net http://www.mysql.com

08 PHP MYSQL Complex Queries Functions

Embed Size (px)

DESCRIPTION

Slide on using PHP and MYSQL queries.

Citation preview

Page 1: 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

Page 2: 08 PHP MYSQL Complex Queries Functions

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 _

Page 3: 08 PHP MYSQL Complex Queries Functions

GeshanManandhar.com 3

tbl_user_test schematbl_user_test schema

Page 4: 08 PHP MYSQL Complex Queries Functions

GeshanManandhar.com 4

tbl_test_user instance/datatbl_test_user instance/data

Page 5: 08 PHP MYSQL Complex Queries Functions

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.

Page 6: 08 PHP MYSQL Complex Queries Functions

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

Page 7: 08 PHP MYSQL Complex Queries Functions

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;

Page 8: 08 PHP MYSQL Complex Queries Functions

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

Page 9: 08 PHP MYSQL Complex Queries Functions

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;

Page 10: 08 PHP MYSQL Complex Queries Functions

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;

Page 11: 08 PHP MYSQL Complex Queries Functions

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;

Page 12: 08 PHP MYSQL Complex Queries Functions

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;

Page 13: 08 PHP MYSQL Complex Queries Functions

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.

Page 14: 08 PHP MYSQL Complex Queries Functions

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.

Page 15: 08 PHP MYSQL Complex Queries Functions

GeshanManandhar.com 15

QuestionsQuestions

Page 16: 08 PHP MYSQL Complex Queries Functions

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…