简体   繁体   English

如果条件为“on”的内部联接查询

[英]Inner join query with “on” if condition

How to write a query to get the below output (see table no. 4)如何编写查询以获得以下 output(见表 4)

table user : role - 1:manager, 2:employee
+----+------+-----------+--------+
| id | name | role      | salary |
+----+------+-----------+--------+
|  1 | a    | 1         |      0 |
|  2 | b    | 1         |      0 |
|  3 | c    | 2         |     10 |
|  4 | d    | 2         |     20 |
|  5 | e    | 2         |     30 |
|  6 | f    | 2         |     40 |
+----+------+-----------+--------+
table city
+----+--------+------+
| id | name   | type |
+----+--------+------+
|  1 | cityA  |    1 |
|  2 | cityB  |    2 |
+----+--------+------+
table user_city_mapping
+----+-----------+---------+
| id | user_id   | city_id |
+----+-----------+---------+
|  1 | 1         | 1       |
|  2 | 2         | 1       |
|  3 | 2         | 2       |
|  4 | 3         | 1       |
|  5 | 4         | 1       |
|  6 | 5         | 2       |
|  7 | 6         | 2       |
+----+-----------+---------+
output required
+------+-------+
| name | total |
+------+-------+
| a    |    30 |
| b    |   100 |
| c    |    10 |
| d    |    20 |
| e    |    70 |
| f    |    70 |
+------+-------+
  • user "a", "b", "c", "d" belongs to "cityA" which is of type 1.用户“a”、“b”、“c”、“d”属于类型 1 的“cityA”。
  • user "b", "e", "f" belong to "cityB" which is of type 2.用户“b”、“e”、“f”属于类型 2 的“cityB”。

  • user "c" and "d" falls under manager "a"用户“c”和“d”属于经理“a”

  • user "c", "d", "e", "f" falls under manager "b"用户“c”、“d”、“e”、“f”属于经理“b”

An explanation for the output required:需要对 output 的解释:

  1. user "a" gets the total 30 which is the sum of "c" and "d" as both "c" and "d" falls under user "a" manager用户“a”得到总数 30,这是“c”和“d”之和,因为“c”和“d”都属于用户“a”经理
  2. user "b" gets the total 100 which is the sum of "c", "d", "e" and "f" as all the users fall under user "b" manager用户“b”得到总数 100,即“c”、“d”、“e”和“f”之和,因为所有用户都属于用户“b”管理器
  3. user "c" and "d" get the total 10 and 20 which is their own salary and belongs to "cityA" which is of "type 1"用户“c”和“d”得到总计 10 和 20,这是他们自己的工资,属于“类型 1”的“cityA”
  4. user "e" and "f" get the total 70 and 70 which is the sum of the salary of employee belongs to "cityB" which if of "type 2"用户“e”和“f”得到总数 70 和 70,即员工工资的总和属于“cityB”,如果属于“type 2”
In short,
if an employee falls under any manager, the manager get the sum of the salary of the employee under him.
if the employee belongs to "type 1" city he gets his salary.
if an employee belongs to the "type 2" city he gets the sum of all employees belongs to that city.

The above-provided details are the requirement and output.以上提供的详细信息是要求和 output。 I am not able to get the query for the desired output.我无法获得所需 output 的查询。

Something which i tried我试过的东西

SELECT b.user_id, sum(salary) 
  FROM user_city_mapping a 
 INNER JOIN user_city_mapping b 
    ON a.city_id = b.city_id 
 INNER JOIN user 
    ON a.user_id = user.id AND role = 2 
 GROUP BY b.user_id

Quite messy and involves parsing the data multiple times.相当混乱,涉及多次解析数据。 Simplest is to work out the salary by user using a correlated sub query based on city type the total salary for type 2 can be calculated.最简单的方法是使用基于城市类型的相关子查询按用户计算工资,可以计算类型 2 的总工资。 Working out the salary by city and who manages that city is achieved in the sub query x by first working out a city total and then aggregating by manager The outer query then selects distinct to get rid of duplicate manager rows.在子查询 x 中按城市计算工资和管理该城市的人是通过首先计算城市总数然后按经理聚合来实现的。外部查询然后选择 distinct 以消除重复的经理行。

select distinct uname,
         case when salarybyuser = 0 then salarybycity
                else salarybyuser
         end salary
from
(
select u.id uid,u.name uname,ucm.*,c.id cid,
         case when c.type = 1 then salary
              when c.type = 2 and role <> 1 then
              (select sum(salary)
                from user u
                join user_city_mapping ucm on ucm.user_id = u.id
                join city c on c.id = ucm.city_id
                where c.type = 2 and role <> 1
                )
         else 0
         end as salarybyuser,
         x.salarybycity
from user u
join user_city_mapping ucm on ucm.user_id = u.id
join city c on c.id = ucm.city_id
left join
(
select ucm.user_id uid,u.name, sum(SalaryByCity) salarybycity
from   user_city_mapping ucm
join   user u on u.id = ucm.user_id 
join
(
select  c1.id cid,sum(salary) SalaryByCity
from user u1
join user_city_mapping ucm1 on ucm1.user_id = u1.id
join city c1 on c1.id = ucm1.city_id
group by c1.id
) s on s.cid = ucm.city_id
where u.role = 1
group by u.id,u.name
) x on x.uid = u.id
order by c.id,u.role,u.id
) y
order by uid
;




+-------+--------+
| uname | salary |
+-------+--------+
| a     |     30 |
| b     |    100 |
| c     |     10 |
| d     |     20 |
| e     |     70 |
| f     |     70 |
+-------+--------+
6 rows in set (0.002 sec)

Seems messy but this is really 3 queries compressed into 1.看起来很乱,但这实际上是将 3 个查询压缩为 1 个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM