在内部联接查询上为一个字段选择“不同的值”

时间:2016-01-05 06:14:28

标签: php mysql join inner-join

我想从两张桌子创建夏日报告。一个表project_type另一个ffw

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   |
|-----------------------------------------------------------------------|
|  ..       | ..            | ..            |     ..          | .....   |
|-----------------------------------------------------------------------|

project_type

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

应用division_id条件后,我在两个表格中得到的结果将是

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

我正在尝试此代码,但它在while循环中显示重复值

$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);

2 个答案:

答案 0 :(得分:6)

您可以使用GROUP BY汇总结果,如下所示:

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

此查询将返回您的记录摘要。

以下是使用MYSQLi面向对象的代码的完整示例:

$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();
  

旁注:我建议使用mysqli_ * OR PDO,因为不推荐使用mysql_ *并且它在PHP 7中不可用

答案 1 :(得分:3)

您可以使用group_by并按以下方式计算:

$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";