获取mysql中不同行的值

时间:2015-05-21 10:14:24

标签: mysql join self

我正在使用MySql,请参阅 http://postimg.org/image/waso0lsnf/ 查看我的表格结构。 我想要提取enduser_to_department = Y department_to_enduser = Y以及department_id.。 对于相同的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

,值可能在不同的行中可用

我尝试过以下方式,但它适用于单行。

:inherited-members:

http://postimg.org/image/waso0lsnf/

修改:从网址添加图片。 enter image description here

3 个答案:

答案 0 :(得分:0)

在where子句中,您可以指定条件

enduser_to_departmen ='Y'和department_to_enduser ='Y'

而不是group by u可以使用distinct

答案 1 :(得分:0)

试试这个:

    SELECT `department_id`
    FROM   `custom_forms_departments` 
    WHERE  `enduser_to_department` = 'Y'
    AND    `department_to_enduser` = 'Y'
    GROUP BY `departments`.`department_id` 
    ORDER BY departments.department_id DESC

编辑:现在不加入。

我刚刚为OR更改了AND,因为您希望它们都是' Y' ,并更改了联接。

答案 2 :(得分:0)

希望这能解决你的问题

SELECT departmentsdepartment_iddepartmentsdepartment 来自custom_forms_departments 左连接departments WHERE departments.department_id = custom_forms_departments.department_id AND(custom_forms_departmentsenduser_to_department ='Y' 或custom_forms_departmentsdepartment_to_enduser ='Y') ORDER BY departments.department_id DESC