Saturday, 12 December 2015

Searching and Sorting of The Data Using MySQL and PHP

Searching Techniques and Sorting Techniques Using MySQL

Prepared By Hitesh Vataliya

Searching is the process of selecting one record or more records from a table or combination of tables and showing as the result into PHP form using MySQL Query.  


To perform the query, three items must be identified:
1. Which fields will be used to identify the records required?
2. Which tables are required to produce the data required?
3. What are the criteria for identifying the records required?

For Example : 
Member is tablename.
Membernumber, forename,surname,birthdate,gender,address,pincode,telephonenumber are fields of table member.

Question 1: 
Identify the names and telephone numbers of club members with Member Number over 1000

Answer: 
Forename, surname and telephone number are selected from table member

SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE `MEMBER NUMBER` > 1000

Question 2:
Select All the fields or columns of the table member.
Answer: 
The asterisk * symbol can be used in an expression string to extract all fields from a table of the database. 

SELECT * FROM MEMBER WHERE `MEMBER NUMBER` > 1000

Question 3: 
Identify the names and telephone numbers of club members with Surnames beginning with M and Member Number over 1000 
Answer : 
SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE SURNAME LIKE “M*” AND `MEMBER NUMBER` > 1000

Question 4: 
Identity the names and telephone numbers of the club members with name begin with N and 3rd character is R and member number>1000.
Answer : 
SELECT FORENAME, SURNAME, `TELEPHONE NUMBER` FROM MEMBER WHERE FORENAME LIKE “N_R*” AND `MEMBER NUMBER` > 1000

Sorting
Sorting in MySQL performed by keyword order by and then asc or desc according to the requirement we can select either asc for ascending order and desc for descending order sorting of the data.

To perform a sort, two items must be identified:
1. Which field (or fields) will be used to decide the order of records?
2. For each field selected, will the order of sorting be ascending or descending?

To produce a list of people with the tallest first, the records would be sorted in descending order of height.  
To produce a list of people with youngest first, the records would be sorted in ascending order of age.
A very common way of ordering records relating to people is in alphabetical order.  To achieve alphabetical ordering requires the records to be sorted in ascending order of surname. 
A complex sort involves more than one sort condition involving two or more fields.
To achieve “telephone book order”, the name is sorted in ascending order of surname, then ascending order of forename.  In this case, the Surname field is the primary sort key, and the Forename field is the secondary sort key.  

For Example :
Question 1: 
Sort Selected Data in Ascending order. 
Answer :
SELECT FORENAME, SURNAME FROM MEMBER 
WHERE `MEMBER NUMBER` > 1000 ORDER BY SURNAME, FORENAME

Question 2: 
Sort Selected Data in Descending order. 
Answer :
SELECT FORENAME, SURNAME FROM MEMBER WHERE `MEMBER NUMBER` > 1000 ORDER BY `MEMBER NUMBER` DESC


For More Information : Website

3 comments:

  1. In Project or Web Development Above code is used for writing query with PHP + MySQL Code.
    For Live Project Training in Vadodara Visit Website: www.vataliyatuitionclasses.com

    ReplyDelete
  2. For PHP Project training in Vadodara, gujarat, india. visit : http://www.vataliyacomputer.in/php-coding-programming-class.php

    ReplyDelete
  3. searcjing for php training in vadodara visit the http://vataliyaclasses2.blogspot.com

    ReplyDelete