【问题标题】:How to take two counts result from second table using MySQL JOIN如何使用 MySQL JOIN 从第二个表中获取两个计数结果
【发布时间】:2017-08-23 05:17:52
【问题描述】:

这里我有两张表,(trip_details & trip_member),我的要求是基于tripId。我想获得员工在场人数和员工缺席人数。在trip_member 表中,我存储了tripId (foriegnkey)、empIdempPresentStatusempPresentStatus= '1' 表示他不在,empPresentStatus ='0' 表示在场。

trip_details

tripId      allocationId       tripStatus

 1             1                  1
 2             1                  1

trip_member

id       tripId          empId       empPresentStatus

 1         1              G2E201        0
 2         1              G2E202        0
 3         1              G2E203        1
 4         2              G2E204        0
 5         2              G2E205        1

根据我的表结构,有多少员工在旅行中,有多少员工在旅行中缺席,我想统计一下。

我试过了

$mysql = mysql_query("SELECT a.tripId, a.cabNo, COUNT('b.*') AS absentCount FROM trip_details a LEFT JOIN trip_member b ON a.tripId = b.tripId WHERE b.empPresentStatus = '1' AND a.tripStatus ='1' GROUP BY a.tripId");
while ($row = mysql_fetch_assoc($mysql)) {
    $data[] = $row;
} // my requirement is based on tripId I want take the employee present count and emplyee absent count, in trip_member table I stored tripId (forienkey),empId,empPresentStatus.here come to know like empPresentStatus= '1' means he is absent, suppose empPresentStatus ='0' means is present.
$arrayName = array('status' => 'success', 'data' =>$data );
echo json_encode($arrayName);

我得到的输出

{
"status": "success",
"data": [
    {
        "tripId": "1",
        "cabNo": "CBX100",
        "absentCount": "1"
    },
    {
        "tripId": "2",
        "cabNo": "CBX101",
        "absentCount": "1"
    }
]
}

到目前为止还可以。我想数一数这次旅行中有多少员工。我不知道如何计算,如果有人知道意味着更新我的答案。

预期结果

{
"status": "success",
"data": [
    {
        "tripId": "1",
        "cabNo": "CBX100",
        "absentCount": "1",
        "presentCount": "2"
    },
    {
        "tripId": "2",
        "cabNo": "CBX101",
        "absentCount": "1",
        "presentCount": "1"
    }
]
}

更新表(cab_allocation)

allocationId     shiftTiming        routeId     cabId
  1                 1                  1        CBX100
  2                 1                  1        CBX101

【问题讨论】:

  • 如果你有另一列 empAbsentStatus 是不是很容易,并且在一个查询中会更容易做到这一点。

标签: php json php-5.6


【解决方案1】:

您可以在此使用子查询。

Change your query with this one.

"SELECT a.tripId, a.cabNo, (select count(*) from trip_member as m WHERE m.tripId=a.tripId and m.empPresentStatus = '1') as presentcount,(select count(*) from trip_member as m WHERE m.tripId=a.tripId and m.empPresentStatus = '0') as absentcount FROM trip_details a WHERE a.tripStatus ='1' GROUP BY a.tripId"

【讨论】:

  • 我试过你的代码我没有得到我的预期结果,我得到了这样的{“status”:“success”,“data”:[{“tripId”:“1”,“cabNo ":"CBX100","presentcount":"1","absentcount":"2" },{"tripId":"2","cabNo":"CBX101","presentcount":"1","absentcount" ": "1" } ] }
  • Trip id 1,应该是这样的 "absentCount": "1" 和 "presentCount": "2",但是你的查询返回错误
  • 只在子查询中改变条件。 "SELECT a.tripId, a.cabNo, (select count() from trip_member as m WHERE m.tripId=a.tripId and m.empPresentStatus = '1') as missingcount,(select count( ) from trip_member as m WHERE m.tripId=a.tripId and m.empPresentStatus = '0') as presentcount FROM trip_details a WHERE a.tripStatus ='1' GROUP BY a.tripId"
  • 我试过我得到 {"status":"success","data":null}
  • 你使用了上面的查询吗?
猜你喜欢
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2014-03-28
  • 1970-01-01
相关资源
最近更新 更多