如何连接两个表并进行数学计算

时间:2017-07-02 05:32:12

标签: mysql sql left-join inner-join

我有两个表,EmployeeDetails和CompOffTable。我想添加CompOff表的行,我得到的每行计数结果应该添加到EmployeeDetails表的列中。我用过Left Out Join。请帮我这个代码。

SELECT 
  a.EmpID, 
  a.EmpName, 
  a.Department, 
  a.Designation, 
  a.TotalLeaves, 
COUNT(b.EmpName) AS TotalCompOff                         
FROM (EmployeeDetails a LEFT OUTER JOIN
(CompOffTable b ON a.EmpName = b.EmpName) 
INNER JOIN SUM(a.TotalLeaves + TotalCompOff) AS RemainingLeaves )                        
GROUP BY a.EmpID, a.EmpName, a.Department, a.Designation, a.TotalLeaves

3 个答案:

答案 0 :(得分:0)

SELECT
      a.empid
    , a.empname
    , a.department
    , a.designation
    , a.totalleaves
    , COUNT(b.empname) AS totalcompoff
    , SUM(a.totalleaves + COUNT(b.empname)) AS remainingleaves
FROM employeedetails a
LEFT OUTER JOIN compofftable b ON a.empname = b.empname
GROUP BY
      a.empid
    , a.empname
    , a.department
    , a.designation
    , a.totalleaves

您无法加入该计算。假设您正在寻找计算,请尝试以下操作,只需将其移回select子句即可。另请注意,您不能在同一个select子句中引用列别名 totalcompoff 。您只需重复该方法即可到达 COUNT(b.empname)列。顺便说一句,这并不会使查询效率降低。

SELECT
      d.empid
    , d.empname
    , d.department
    , d.designation
    , d.totalleaves
    , d.totalcompoff
    , SUM(d.totalleaves + d.totalcompoff) AS remainingleaves
FROM (
      SELECT
            a.empid
          , a.empname
          , a.department
          , a.designation
          , a.totalleaves
          , COUNT(b.empname) AS totalcompoff
      FROM employeedetails a
      LEFT OUTER JOIN compofftable b ON a.empname = b.empname
      GROUP BY
            a.empid
          , a.empname
          , a.department
          , a.designation
          , a.totalleaves
      ) d

如果db类型/版本不允许该构造,则使用派生表方法,在这里您可以重用列别名:

{{1}}

答案 1 :(得分:0)

如果数据看起来像这样

MariaDB [sandbox]> select e.emp_no,e.last_name, e.holiday_entitlement
    -> from employees e;
+--------+-----------+---------------------+
| emp_no | last_name | holiday_entitlement |
+--------+-----------+---------------------+
|      1 | AAA       |                  30 |
|      2 | BBB       |                  30 |
|      3 | CCC       |                  30 |
|      4 | DDD       |                  30 |
|      5 | EEE       |                  30 |
|      6 | FFF       |                  30 |
|      7 | GGG       |                  30 |
+--------+-----------+---------------------+
7 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select id , entity_id , date_from , date_to
    -> from holiday_table;
+------+-----------+------------+------------+
| id   | entity_id | date_from  | date_to    |
+------+-----------+------------+------------+
|    1 |         1 | 2017-01-01 | 2017-01-01 |
|    2 |         1 | 2017-02-01 | 2017-02-01 |
|    3 |         2 | 2017-01-01 | 2017-01-01 |
+------+-----------+------------+------------+
3 rows in set (0.00 sec)

然后这可能会

MariaDB [sandbox]> select e.emp_no,e.last_name, e.holiday_entitlement
    -> ,ifnull((select sum(datediff(date_to,date_from) + 1) from holiday_table h where h.entity_id = e.emp_no), e.holiday_entitlement) entitlement
    ->    , ifnull(e.holiday_entitlement - (select sum(datediff(date_to,date_from) + 1) from holiday_table h where h.entity_id = e.emp_no),0) LeaveRemaining
    -> from employees e;
+--------+-----------+---------------------+-------------+----------------+
| emp_no | last_name | holiday_entitlement | entitlement | LeaveRemaining |
+--------+-----------+---------------------+-------------+----------------+
|      1 | AAA       |                  30 |           2 |             28 |
|      2 | BBB       |                  30 |           1 |             29 |
|      3 | CCC       |                  30 |          30 |              0 |
|      4 | DDD       |                  30 |          30 |              0 |
|      5 | EEE       |                  30 |          30 |              0 |
|      6 | FFF       |                  30 |          30 |              0 |
|      7 | GGG       |                  30 |          30 |              0 |
+--------+-----------+---------------------+-------------+----------------+
7 rows in set (0.00 sec)

答案 2 :(得分:0)

这可能是我在你的帮助下找到的正确答案

@media screen and (min-width: 599px) {
    .sec-1 h2 {
        font-size: 1.2em;
        background-color: green;
    }

    .sec-1 p {
        font-size: 1.1em;
        width: 50%;
        background-color: yellow;
    }

    .sec-1 .phone-img {
        position: relative;
        top: 10%;
        left: 30%;
        background-color: purple;

    } 
    .download-btns {
        position: relative;
        right: 25%;
        background-color: orange;
    }

    .sec-1 .sales-copy {
        width: 50%;
    }


    .flex-box {
        display: flex;
           justify-content: flex-end;

    }

}