在mysql中的SELECT语句

时间:2015-12-20 07:54:01

标签: mysql sql

如何选择列workspace.assigned_to = NULL ELSE不选择任何内容。

SELECT workspace.*, projects.*,
  CASE 
    WHEN workspace.assigned_to != NULL 
    THEN users.user_id,users.first_name, users.last_name 
   END FROM workspace
INNER JOIN projects ON workspace.workspace_id = projects.project_id 
INNER JOIN users ON workspace.assigned_to = users.user_id
WHERE project_id=:project_id

1 个答案:

答案 0 :(得分:0)

SELECT workspace.*, projects.*,
  users.user_id,users.first_name, users.last_name
FROM workspace
INNER JOIN projects ON workspace.workspace_id = projects.project_id 
LEFT JOIN users ON workspace.assigned_to = users.user_id
WHERE project_id=:project_id

LEFT JOIN表示我们通过assigned_to加入用户,但如果它为null,则用户的valuea为空