使用所有运算符选择子查询而不加入mysql

时间:2018-03-23 12:57:58

标签: mysql sql subquery operators mariadb

我需要显示来自部门1的所有工作人员的ID,姓名和出生日期,他们比部门2的工人年轻。我正在尝试使用而不用加入。

这就是我现在所拥有的,当我运行它时它不会失败,但它会显示来自第2部门的所有工人,无论他们的生日如何。

SELECT ID, CONCAT(fName," " ,lName) AS "Worker Name", dob
FROM Worker
WHERE dob <= ALL (SELECT department
FROM Worker
WHERE deparment = 1 
)
AND deparment = 2;

2 个答案:

答案 0 :(得分:0)

你想要检查dob然后你应该选择dob

  SELECT ID, CONCAT(fName," " ,lName) AS "Worker Name", dob
  FROM Worker
  WHERE dob <= ALL (SELECT dob
  FROM Worker
  WHERE deparment = 1 
  )
  AND deparment = 2;

答案 1 :(得分:0)

这应列出来自department1的所有来自第2部门工作人员的年轻工人:

SELECT ID, CONCAT(fName," " ,lName) AS "Worker Name", dob
FROM Worker
WHERE dob <  (SELECT min(w2.dob)
FROM Worker w2
WHERE w2.deparment = 2
)
AND deparment = 1