如何获取与值匹配或存在于另一个表中的记录? [英] How to get records that match value or exist in another table?

查看:28
本文介绍了如何获取与值匹配或存在于另一个表中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我试图弄清楚如何获得所有任务,即两个字段等于某个值或它们存在于另一个表中?

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?

这是查询:

SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND 
      (TASKS.task_creator_id = ? OR 
       TASKS.task_assigned_user_id = ? OR
       WHERE EXISTS (SELECT  WATCHERS.task_id
                     FROM WATCHERS
                     WHERE WATCHERS.task_id = TASK.task_id AND
                           WATCHERS.watcher_user_id = ?
                    )
     );

即使我期望数据库返回结果,也不会返回任何内容.

This is not returning anything even though I am expecting a result from my db.

推荐答案

此sql与您在具有示例数据和结果的重复帖子中发布的结果集相匹配:

This sql matches the result set you posted in your duplicate post that has sample data and result:

SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
      (
      t.task_creator_id = 8
      OR t.task_assigned_user_id = 8
      OR EXISTS
          (
          SELECT w.task_id
          FROM watchers w
          WHERE w.task_id = t.task_id
          AND w.watcher_user_id = 8
          )
      )

这篇关于如何获取与值匹配或存在于另一个表中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆