• Save
08 Php Mysql Complex Queries Functions
Upcoming SlideShare
Loading in...5
×
 

08 Php Mysql Complex Queries Functions

on

  • 5,136 views

Complex MYSQL queries from PHP

Complex MYSQL queries from PHP

Statistics

Views

Total Views
5,136
Views on SlideShare
5,122
Embed Views
14

Actions

Likes
1
Downloads
0
Comments
0

4 Embeds 14

http://www.slideshare.net 10
http://www.linkedin.com 2
http://translate.googleusercontent.com 1
http://www.twylah.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

08 Php Mysql Complex Queries Functions 08 Php Mysql Complex Queries Functions Presentation Transcript

  • PHP Day 08 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com/slides/php http://www.php.net http://www.mysql.com
  • 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 _
  • tbl_user_test schema
  • tbl_test_user instance/data
  • 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.
  • Limit in MYSQL
    • SELECT * FROM `tbl_user_test` LIMIT 1 ; - select only one row.
    • SELECT * FROM `tbl_user_test` LIMIT 1,3 ; - Select 2 nd row and 3 rows including 2 nd row.
      • Good to use for pagination
  • Table joins with MYSQL
    • SELECT tu.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;
  • 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
    • SELECT tp.type, COUNT( tp.name ) FROM tbl_products AS tp GROUP BY tp.type;
  • 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;
  • String Concat
    • SELECT CONCAT(tc.first_name, ' ', tc.middle_name, ' ', tc.last_name) AS FullName FROM tbl_client AS tc;
  • CONCAT_WS
    • SELECT CONCAT_WS(' ', tc.first_name, tc.middle_name, tc.last_name) AS FullName FROM tbl_client AS tc;
  • Select for searches
    • SELECT * FROM `tbl_user_test` WHERE user_login LIKE 'user%' ;
    • %: Matches any number of characters, even zero characters.
  • Select for searches
    • SELECT * FROM `tbl_user_test` WHERE user_login LIKE 'user_' ;
    • _: Matches exactly one character.
  • Questions
  • 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…