Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Distinct value for one field on Inner Join Query

I want to create summery report from two tables. one table project_type another ffw

Table ffw :

|-----------------------------------------------------------------------|
| ffw_id    | division_id   | district_id   | project_type_id | name
|-----------------------------------------------------------------------|
| 1         | 30            | 1             |     2           |myAddress
|-----------------------------------------------------------------------|
| 2         | 12            | 2             |     1           | Asdfads |
|-----------------------------------------------------------------------|
| 3         | 30            | 6             |     1           | kkkkk   |
|-----------------------------------------------------------------------|
|  ..       | ..            | ..            |     ..          | .....   |
|-----------------------------------------------------------------------|

Table project_type:

|--------------------------------
| project_type_id | project_type | 
|--------------------------------|
| 1               | food         |
|--------------------------------|
| 2               | work         |
|--------------------------------|
| 3               | visit        |
|--------------------------------|
| ..              | ..           |
|--------------------------------|

My desired result from that two tables after applying division_id condition will be

|-------------------------------------------|
| no        | project_type  | count         | 
|-------------------------------------------|
| 1         | food          | 2             |
|-------------------------------------------|
| 2         | work          | 1             |
|-------------------------------------------|
| 3         | visit         | .             |
|-------------------------------------------|
|  ..       | ..            | ..            |
|-------------------------------------------|

I am trying this code but it displaying duplicate value in while loop

$qry = "
    SELECT * FROM `project_type` 
    LEFT JOIN `ffw` 
        ON project_type.project_type_id = ffw.project_type_id
    WHERE 1
";

if (strlen($_POST["division_id"]) > 0 && $_POST["division_id"] != "0")
{
    $qry .= " AND division_id = '".$_POST["division_id"]."'";
}

$query = mysql_query($qry);
like image 531
Lemon Kazi Avatar asked Jan 06 '23 21:01

Lemon Kazi


2 Answers

You can use GROUP BY for summarize the result as like that:

SELECT pt.project_type, count(*) as count FROM project_type pt
LEFT JOIN ffw ff ON ff.project_type_id = pt.project_type_id
GROUP BY pt.project_type

This query will return you the summary of your records.

Here is the complete example of your code by using MYSQLi Object Oriented:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT pt.project_type, count(*) as count FROM project_type pt
LEFT JOIN ffw ff ON ff.project_type_id = pt.project_type_id
WHERE 1 = 1 
";


if(strlen($_POST["division_id"])> 0 && $_POST["division_id"]!="0")
{
    $sql.= " AND division_id = '".$_POST["division_id"]."'";
}

$sql .= "GROUP BY pt.project_type";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $i = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $id. " - Name: " . $row["project_type"]. " " . $row["count"]. "<br>";
        $i++;
    }
} 
else 
{
    echo "0 results";
}
$conn->close();

Side Note: I suggest to use mysqli_* OR PDO, because mysql_* is deprecated and its not available in PHP 7

like image 128
devpro Avatar answered Jan 16 '23 20:01

devpro


You can use group_by and get count as below :

$qry="SELECT pt.project_type_id as no, pt.project_type, count(*) as count FROM project_type pt LEFT JOIN ffw ON pt.project_type_id=ffw.project_type_id";

if(strlen($_POST["division_id"])>0&&$_POST["division_id"]!="0")
{
     $qry.=" Where division_id='".$_POST["division_id"]."'";
}

$qry.=" GROUP BY pt.project_type";
like image 22
AnkiiG Avatar answered Jan 16 '23 21:01

AnkiiG