mysql 查询从子查询迁移

标签 mysql sql

您好,我有以下工作查询,但我希望摆脱子查询方法。

select t0.emp_id, t1.first_name, t1.last_name

from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id

where t0.emp_id not in (select t4.emp_id from time_sheet as t4 where t4.ts_end_date = '2014-10-08')

共有 1301 名员工资料 有 337 个没有结束日期 2014-10-08 的 time_sheets

到目前为止,这工作得很好。

现在我正在尝试从子查询迁移到联接。我尝试了以下方法但没有成功。

通过此查询,我得到的结果与我正在寻找的结果完全相反,即 964 个结果。

select t0.emp_id, t1.first_name, t1.last_name, t3.emp_id

from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id 
left join time_sheet as t3 on t0.id = t3.employee_profile_id

我还尝试了以下使用 where 条件并得到 0 个结果

1.

where t3.ts_end_date = '2014-10-08'
and t3.employee_profile_id is null

2.

where t3.ts_end_date = '2014-10-08'
and t3.id is null

有人知道如何解决这个问题吗?

最佳答案

明白了

select t0.emp_id, t1.first_name, t1.last_name, t3.emp_id

from employee_profile As t0 left join user_profile as t1 on t0.user_profile_id = t1.id 
left join time_sheet as t3 on t0.id = t3.employee_profile_id and t3.ts_end_date = '2014-10-08'

where t3.employee_profile_id is null

关于mysql 查询从子查询迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26849759/

相关文章:

sql - 创建没有数字主键的 SQL 数据库表是不好的做法吗?

php - 如何使用 PHP 创建搜索按钮

mysql - 使用连接表 Mysql 按同一列排序

PHP & MySQL 页面速度

php - 在 SELECT 查询之后的 while 循环之前检查 num_rows 有什么用处?

mysql - 如何用MySQL中以前的非零值替换零值?

mysql - 如何获取特定列中非 NULL 行的百分比?

PHP 自动生成的 MySQL Create Table 语句返回错误

python - 如何删除表中的列,其中列名是mysql中的 "index"和 "Unnamed: 0"

mysql - 计算SQL中以下记录之间的距离