MYSQL显示左边的Null员工加入

时间:2014-08-06 05:29:18

标签: mysql sql

我在如何在此查询中显示所有空值时遇到困难

这是我的SQL fiddle

 SELECT project_staff_assignment.taxemployee_id, taxemployee.nickname
  FROM taxemployee 
  LEFT JOIN project_staff_assignment ON project_staff_assignment.taxemployee_id = taxemployee.id
 WHERE taxemployee.id = 4

我想看到的还显示其他员工的空值

2 个答案:

答案 0 :(得分:1)

可能是这样的

SELECT project_staff_assignment.taxemployee_id, taxemployee.nickname
FROM taxemployee 
LEFT JOIN project_staff_assignment ON project_staff_assignment.taxemployee_id = taxemployee.id
WHERE taxemployee.id = 4 OR project_staff_assignment.taxemployee_id IS NULL

<强> FIDDLE DEMO

答案 1 :(得分:0)

如果我理解正确,this就是您所需要的。 删除condtion。它将仅返回1条记录,因为您明确过滤了id = 4

 SELECT project_staff_assignment.taxemployee_id, taxemployee.nickname
  FROM taxemployee 
  LEFT JOIN project_staff_assignment ON project_staff_assignment.taxemployee_id = taxemployee.id

UPDATED FIDDLE

 SELECT 
CASE project_staff_assignment.taxemployee_id WHEN '4' THEN project_staff_assignment.taxemployee_id ELSE NULL END AS REQUIRED_FIELD,
taxemployee.id,`EmployeeNumber`, `Fullname`, `Nickname`,`Position`, `level`
  FROM taxemployee 
  LEFT  JOIN project_staff_assignment  ON project_staff_assignment.taxemployee_id = taxemployee.id

正在做的是,选择所有记录,并使用CASE STATEMENT,如果值= 4,则显示为4,否则显示为NULL。