- function get_enum_values($table, $field)
- {
- $sql = "SHOW COLUMNS FROM $table WHERE Field = '$field'";
- $type = $row['Type'];
- return $enum;
- }
- function get_enum_values($table, $field)
- {
- $sql = "SHOW COLUMNS FROM $table WHERE Field = '$field'";
- $type = $row['Type'];
- return $enum;
- }
| KEY NAMES | KEY DESCRIPTION |
|---|---|
| PRIMARY KEY | Name of the column which uniquely identifies each row of the table with no null value in any row of the table. |
| UNIQUE KEY | Name of the column which uniquely identifies each row of the table, but it can accept a null value for any row of the table. |
| COMPOSITE KEY | The combination of two or more column representing as a unique for each row of the table after merging the columns. It acts like a single column in contest of the key. |
| SURROGATE KEY | A key which has no buisness logic or which have no existance in the real world. for more clarity see NATURAL KEY |
| FOREIGN KEY | Name of the column which have the reference of the other table by having the same value of any column other table. |
| CANDIDATE KEY | Any key which can represent as unique for each row is called CANDIDATE KEY. Hence PRIMARY KEY, UNIQUE KEY and COMPOSITE KEY are a member of CANDIDATE KEY |
| NATURAL KEY | A key which have a real existinence in business logic or have the real value in the world like AADHAR NO in India and SSN NO in USA |
Sometime you’d stuck like that you’ll need all other rows coming with left join as a duplicate row for all record, where you would need to have all these inside an array variable. For example if you have an tbl_emp and another tbl_contact, in tbl_contact you would have one foreign key emp_id.
You made a query to fetch all employee record with their contact nos, when you left join the query such like LEFT JOIN tbl_contact ON tbl_contact.emp_id=tbl_emp.emp_id, you will get record in associative array something like this.
array (size=3) 0 => array (size=3) 'name' => string 'test1' (length=5) 'age' => int 23 'email' => string 'Good' (length=4) 1 => array (size=3) 'name' => string 'test2' (length=5) 'age' => int 24 'email' => string 'Good' (length=4) 2 => array (size=3) 'name' => string 'test2' (length=5) 'age' => int 24 'email' => string 'Best' (length=4)
Here you are getting two record for test2 employee, because this employee has two email as a contact.
So, to simplify this you will need to have all contact nos inside a single variable key and the duplicate record to be removed, then here is the logic to do that.
- <?php
- for($i=0;$i<$total_size;$i++){
- $arr[$i]['emails'][]=$arr[$i]['email'];
- $unset=0;
- foreach($arr[$i] as $key=>$value){
- if($key=='name' && $arr[$i]['name']==$arr[$i+1]['name']){
- $arr[$i]['emails'][]=$arr[$i+1]['email'];
- $unset=1;
- }
- }
- if($unset==1){
- $i++;
- }
- }
- ?>
Now you’ll get the result something like that
array (size=2) 0 => array (size=3) 'name' => string 'test1' (length=5) 'age' => int 23 'emails' => array (size=1) 0 => string 'Good' (length=4) 1 => array (size=3) 'name' => string 'test2' (length=5) 'age' => int 24 'emails' => array (size=2) 0 => string 'Good' (length=4) 1 => string 'Best' (length=4)
where clause can not accept aggregate function like sum, count, etc. whereas having clause can accept the aggregate functions like sum, count, average, etc.
Let’s see it by example.
| emp_salary_paid | |||
| paid_id | emp_id | salary_given | salary_month |
|---|---|---|---|
| 1 | 1 | 1000 | jan |
| 2 | 2 | 1500 | jan |
| 3 | 3 | 2000 | jan |
| 4 | 1 | 1200 | feb |
| 5 | 2 | 1500 | feb |
| 6 | 3 | 1800 | feb |
| emp_detail | |
| emp_id | emp_name |
|---|---|
| 1 | Sajid |
| 2 | John |
| 3 | Ahmad |
Query:
- SELECT emp_detail.emp_name, emp_salary_paid.salary_month, emp_salary_paid.salary_given
- FROM emp_detail, emp_salary_paid
- WHERE emp_detail.emp_id=emp_salary_paid.emp_id
will produce result like this one. because no aggregate function is being used here.
| emp_name | salary_month | salary_given |
|---|---|---|
| Sajid | jan | 1000 |
| Sajid | feb | 1200 |
| John | jan | 1500 |
| John | feb | 1500 |
| Ahmad | jan | 2000 |
| Ahmad | feb | 1800 |
- SELECT emp_detail.emp_name, SUM(emp_salary_paid.salary_given) AS total_paid
- FROM emp_detail, emp_salary_paid
- GROUP BY emp_detail.emp_id
- WHERE SUM(emp_salary_paid.salary_given) >2000
This will produce an error, because aggregate function is being used with where clause. To overcome this, we use having clause like this one.
- SELECT emp_detail.emp_name, SUM(emp_salary_paid.salary_given)
- FROM emp_detail, emp_salary_paid
- GROUP BY emp_detail.emp_id
- HAVING SUM(emp_salary_paid.salary_given) > 2000
This will produce the correct result as follows:
| emp_name | total_paid |
|---|---|
| Sajid | 9000 |
| John | 9000 |
| Ahmad | 9000 |