联接另一张桌子和同一张桌子

时间:2018-09-21 10:06:49

标签: mysql sql

需要您的帮助 我一直在为这一查询工作一天

我有2张桌子

表用户

enter image description here

和表payroll_variable

enter image description here

我在这里的目标是加入具有2个或更多表payroll_variable的表用户,像这样

enter image description here

这是我到目前为止所做的:

      SELECT  DISTINCT
                u.id_user,
                u.first_name,
                u.last_name,
                pv.year,
                pv.base_salary,
                CASE
                    WHEN pv.month = 'January' THEN pv.total_salary
                    ELSE NULL
                END as 'January',
                CASE
                    WHEN pv1.month = 'February' THEN pv1.total_salary
                    ELSE NULL
                END as 'February'
        FROM payroll_variable pv
        INNER JOIN users u ON u.id_user = pv.id_user
        INNER JOIN payroll_variable pv1 ON  pv1.id_user = pv.id_user
        WHERE pv.status != 'submit'

这是我查询的错误结果

enter image description here

问题是,我只想向每个用户显示仅一个循环(如我的目标),并且不介意我的WHERE子句,它只是用于标记

2 个答案:

答案 0 :(得分:0)

问题是,当一月或二月没有付款时,您不能确保您有记录,并且也不能确保一月和二月的每个逻辑对都具有相同的记录用户。您将需要使用LEFT JOIN来确保每个用户都有一行,并且需要确保每个逻辑元组只有一行。我不了解您的where条件背后的逻辑,因此我在这里可能是错的。我假设如果有付款,则只记录一月或二月的记录:

  SELECT  DISTINCT
            u.id_user,
            u.first_name,
            u.last_name,
            pv.year as year,
            pv.base_salary,
            CASE
                WHEN pv.month = 'January' THEN pv.total_salary
                ELSE 0
            END as January,
            CASE
                WHEN pv.month = 'February' THEN pv.total_salary
                ELSE 0
            END as February,
    FROM users u
    LEFT JOIN payroll_variable jan ON u.id_user = jan.id_user
    LEFT JOIN payroll_variable feb ON  feb.id_user = u.id_user
    WHERE jan.status <> 'submit' and feb.status <> 'submit' and (not (jan.id_user is null) or not (feb.id_user is null))

此代码未经测试,可能是我误解了您的意图。

编辑

要按用户对结果进行分组,您需要使用分组依据:

select id_user, first_name, last_name, max(t.year), max(base_salary), sum(January) as January, sum(February) as February
from (
      SELECT
                u.id_user,
                u.first_name,
                u.last_name,
                min(pv.year) as year,
                min(pv.base_salary) as base_salary,
                sum(case when pv.payroll_month = 'January' then pv.total_salary else 0 end) as January,
                sum(case when pv.payroll_month = 'February' then pv.total_salary else 0 end) as February
        FROM users u
        LEFT JOIN payroll_variable pv ON u.id_user = pv.id_user) t
        group by t.id_user, t.first_name, t.last_name

答案 1 :(得分:0)

首先,我将查询中的两个字段的名称(即月份和年份)更改为payroll_month和payroll_year。

这是为您提供所需输出的查询

Select u.id_user, u.first_name, u.last_name, pv.payroll_year, pv.base_salary,
sum(IF(pv.payroll_month = 'January', pv.total_salary, 0)) as 'January',
sum(IF(pv.payroll_month = 'February', pv.total_salary, 0)) as 'February',
sum(IF(pv.payroll_month = 'March', pv.total_salary, 0)) as 'March',
sum(IF(pv.payroll_month = 'April', pv.total_salary, 0)) as 'April',
sum(IF(pv.payroll_month = 'May', pv.total_salary, 0)) as 'May',
sum(IF(pv.payroll_month = 'June', pv.total_salary, 0)) as 'June',
sum(IF(pv.payroll_month = 'July', pv.total_salary, 0)) as 'July',
sum(IF(pv.payroll_month = 'August', pv.total_salary, 0)) as 'August',
sum(IF(pv.payroll_month = 'September', pv.total_salary, 0)) as 'September',
sum(IF(pv.payroll_month = 'October', pv.total_salary, 0)) as 'October',
sum(IF(pv.payroll_month = 'November', pv.total_salary, 0)) as 'November',
sum(IF(pv.payroll_month = 'December', pv.total_salary, 0)) as 'December'
from users u join payroll_variable pv using(id_user)
group by u.id_user

And sample output as in image