08 Php Mysql Complex Queries Functions

3,592 views
3,495 views

Published on

Complex MYSQL queries from PHP

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,592
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

08 Php Mysql Complex Queries Functions

  1. 1. PHP Day 08 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com/slides/php http://www.php.net http://www.mysql.com
  2. 2. MYSQL queries <ul><li>MYSQL query with sorting ASC and DESC </li></ul><ul><li>MYSQL query with limit and offset </li></ul><ul><li>MYSQL query with join 2 tables </li></ul><ul><li>MYSQL query with count, sum, avg, min </li></ul><ul><li>MYSQL query with string concat </li></ul><ul><li>MYSQL query with like % and _ </li></ul>
  3. 3. tbl_user_test schema
  4. 4. tbl_test_user instance/data
  5. 5. Sorting With MYSQL <ul><li>SELECT tu.user_id, tu.user_login, tu.email FROM tbl_user_test AS tu ORDER BY tu.user_login ASC ; </li></ul><ul><li>SELECT tu.user_id, tu.user_login, tu.email FROM tbl_user_test as tu ORDER BY tu.email DESC ; </li></ul><ul><li>ASC – Ascending taken by default. </li></ul>
  6. 6. Limit in MYSQL <ul><li>SELECT * FROM `tbl_user_test` LIMIT 1 ; - select only one row. </li></ul><ul><li>SELECT * FROM `tbl_user_test` LIMIT 1,3 ; - Select 2 nd row and 3 rows including 2 nd row. </li></ul><ul><ul><li>Good to use for pagination </li></ul></ul>
  7. 7. Table joins with MYSQL <ul><li>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; </li></ul>
  8. 8. Calculations with MYSQL <ul><li>SELECT MIN( tp.price ) FROM tbl_products AS tp; - Applicable of single column </li></ul><ul><li>SELECT tp.type, MIN( tp.price ) FROM tbl_products AS tp GROUP BY tp.type; - simple group by. </li></ul><ul><li>SELECT COUNT( * ) FROM tbl_products; - single column </li></ul>
  9. 9. <ul><li>SELECT tp.type, COUNT( tp.name ) FROM tbl_products AS tp GROUP BY tp.type; </li></ul>
  10. 10. Sum and Average <ul><li>SELECT SUM ( tp.price ) FROM tbl_products AS tp; </li></ul><ul><li>SELECT tp.type, SUM (tp.price) FROM tbl_products AS tp GROUP BY tp.type; </li></ul><ul><li>SELECT AVG ( tp.price ) FROM tbl_products AS tp; </li></ul><ul><li>SELECT tp.type, AVG (tp.price) FROM tbl_products AS tp GROUP BY tp.type; </li></ul>
  11. 11. String Concat <ul><li>SELECT CONCAT(tc.first_name, ' ', tc.middle_name, ' ', tc.last_name) AS FullName FROM tbl_client AS tc; </li></ul>
  12. 12. CONCAT_WS <ul><li>SELECT CONCAT_WS(' ', tc.first_name, tc.middle_name, tc.last_name) AS FullName FROM tbl_client AS tc; </li></ul>
  13. 13. Select for searches <ul><li>SELECT * FROM `tbl_user_test` WHERE user_login LIKE 'user%' ; </li></ul><ul><li>%: Matches any number of characters, even zero characters. </li></ul>
  14. 14. Select for searches <ul><li>SELECT * FROM `tbl_user_test` WHERE user_login LIKE 'user_' ; </li></ul><ul><li>_: Matches exactly one character. </li></ul>
  15. 15. Questions
  16. 16. Lets run some SQL queries <ul><li>Show users in ascending and descending order as per user_login. </li></ul><ul><li>Show users of row 2-5 in ascending order as per email. </li></ul><ul><li>Show maximum price from tbl_products grouped by type. </li></ul><ul><li>Experiment concat and search queries… </li></ul>

×