Mysql查询从id获取不同列的记录

时间:2015-05-22 04:38:22

标签: mysql join

以下是我的表格结构的详细信息。

表结构“部门”:

id  |   department
1   |   Department 1
2   |   Department 2
3   |   Department 3
4   |   Department 4

表结构“custom_forms_departments”:

id  |   form_id | department_id | enduser_to_department | department_to_enduser
1   |   5       |   1           |       Y               |       N
2   |   6       |   1           |       N               |       Y
3   |   8       |   2           |       Y               |       Y
4   |   7       |   3           |       N               |       Y
5   |   4       |   3           |       Y               |       N
6   |   2       |   4           |       N               |       N

结果应该返回department_id,对于同一行或不同行中的字段“enduser_to_department”和“department_to_enduser”,其值为“Y”。

department_id = 1个不同行中“enduser_to_department”和“department_to_enduser”的值“Y” department_id = 2个contatin值“Y”表示“enduser_to_department”和“department_to_enduser”在同一行中 department_id = 3个不同行中“enduser_to_department”和“department_to_enduser”的值“Y”

结果:

  department_id | departments
    1           |   Department 1
    2           |   Department 2
    3           |   Deapartment 3

我正在使用以下SQL查询,但没有给出正确的结果。

SELECT departments.department_id, departments.department 
FROM custom_forms_departments , departments
WHERE departments.department_id = custom_forms_departments.department_id
AND (custom_forms_departments.enduser_to_department = 'Y'
OR  custom_forms_departments.department_to_enduser = 'Y')
GROUP BY departments.department_id 
ORDER BY departments.department_id DESC

请建议我这个。

3 个答案:

答案 0 :(得分:1)

首先尝试照顾custom_forms_departments

方式:创建custom_forms_departments(c1和c2)的2个副本。你想要基于(c1.department_id = c2.department_id) - 简单和(c1.enduser_to_department = c2.department_to_enduser)来加入它们 - 因为你只想获得其中同时包含'Y'的行(将过滤'Y' '在WHEN中,但是现在,你将获得两列中具有相同值的任何行)。其次,使用WHEN仅过滤'Y'。

SELECT 
    custom_forms_departments.department_id
FROM
    custom_forms_departments c1
        INNER JOIN
    custom_forms_departments c2 ON c1.department_id = c2.department_id
        AND c1.enduser_to_department = c2.department_to_enduser
WHERE
    c1.enduser_to_department = 'Y'
GROUP BY
    c1.department_id 
;

现在我们有了“复杂”的工作人员,让我们聚在一起并添加departments列:

SELECT 
    departments.department_id, departments.department
FROM
    departments
        INNER JOIN
    (SELECT 
        custom_forms_departments.department_id
    FROM
        custom_forms_departments c1
    INNER JOIN custom_forms_departments c2 ON c1.department_id = c2.department_id
        AND c1.enduser_to_department = c2.department_to_enduser
    WHERE
        c1.enduser_to_department = 'Y'
    GROUP BY
        c1.department_id) c3 ON departments.department_id = c3.department_id
;

答案 1 :(得分:0)

使用以下查询。也始终使用显式JOIN

SELECT d.department_id, d.department 
FROM custom_forms_departments AS cfd
INNER JOIN departments AS d ON d.department_id = cfd.department_id
AND (cfd.enduser_to_department = 'Y' OR cfd.department_to_enduser = 'Y')
GROUP BY d.department_id, d.department 
ORDER BY d.department_id

答案 2 :(得分:0)

SELECT id as department_id, department from departments WHERE id IN 
(
SELECT DISTINCT(departament_id) FROM custom_forms_departments WHERE enduser_to_department = 'Y' OR department_to_enduser = 'Y'
)