MySQL:如果在哪里?

发布于 2024-12-29 04:58:43 字数 540 浏览 2 评论 0原文

我的查询从 MySQL 数据库中选择任务。每个任务都有一个组 ID,如果该组等于 0,则它是一个私有任务,如果该任务来自运行此查询的用户,我只想返回。希望这是有道理的:-)

SELECT t.id, t.title, t.created_by, u.username
FROM (p_tasks AS t)
LEFT JOIN p_groups AS g ON t.group_id = g.id
JOIN p_users AS u ON t.assigned_to_user_id = u.id
WHERE 
 (
   t.assigned_to_user_id = $user_id OR
   t.owner_user_id = $user_id
 )
 AND
 (
   g.priority >= '10' OR
   t.group_id = 0
 )
 AND
 (
   t.status != "approved" AND
   t.status != "closed"
 )
ORDER BY c.name

如何只为创建任务的人返回 group_id = 0 的任务?

My query selects tasks from a MySQL DB. Each task has a group id and if the group is equal 0 it is a private task I want only return, if the task is from the user who runs this query. Hope that makes sense :-)

SELECT t.id, t.title, t.created_by, u.username
FROM (p_tasks AS t)
LEFT JOIN p_groups AS g ON t.group_id = g.id
JOIN p_users AS u ON t.assigned_to_user_id = u.id
WHERE 
 (
   t.assigned_to_user_id = $user_id OR
   t.owner_user_id = $user_id
 )
 AND
 (
   g.priority >= '10' OR
   t.group_id = 0
 )
 AND
 (
   t.status != "approved" AND
   t.status != "closed"
 )
ORDER BY c.name

How can I return the tasks with group_id = 0 only for those who had created the tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不疑不惑不回忆 2025-01-05 04:58:43

不确定我完全理解您的查询,但您描述的部分看起来像这样。这假设您希望将所有公共任务和所有私人任务分配给用户或由用户拥有。

WHERE  
 ( 
   (t.assigned_to_user_id = $user_id OR t.owner_user_id = $user_id) AND 
   t.group_id = 0  
 ) 
 OR 
 ( 
    t.group_id != 0    
 )

Not sure I understand your query completely, but the part you have described would look something like this. This assumes you want all public tasks and all private ones assigned to the user or owned by the user.

WHERE  
 ( 
   (t.assigned_to_user_id = $user_id OR t.owner_user_id = $user_id) AND 
   t.group_id = 0  
 ) 
 OR 
 ( 
    t.group_id != 0    
 )
南风几经秋 2025-01-05 04:58:43
(group_id <> 0 OR t.owner_user_id = $user_id) AND
t.assigned_to_user_id = $user_id AND
t.status <> "approved" AND
t.status <> "closed"

您应该能够在不使用 IF 语句的情况下构造 where 子句。

(group_id <> 0 OR t.owner_user_id = $user_id) AND
t.assigned_to_user_id = $user_id AND
t.status <> "approved" AND
t.status <> "closed"

You should be able to construct your where clause without using an IF statement.

我不完全理解你的意思,但就像英语一样,清晰的语法是:

where 
(... AND ... AND ...)
or
(... AND ... AND ...)
or
(... AND ... AND ...)

I do not full understud what you mean, but like in english, the clear syntax is:

where 
(... AND ... AND ...)
or
(... AND ... AND ...)
or
(... AND ... AND ...)
影子的影子 2025-01-05 04:58:43
SELECT t.id, t.title, t.created_by, u.username
FROM (p_tasks AS t)
LEFT JOIN p_groups AS g ON t.group_id = g.id
JOIN p_users AS u ON t.assigned_to_user_id = u.id
WHERE 
 (
   t.assigned_to_user_id = $user_id OR
   t.owner_user_id = $user_id
 )
 AND
 (
   g.priority >= '10' OR
   t.group_id = 0
 )
 AND
 (
   t.status != "approved" AND
   t.status != "closed"
 )
 AND 
 (
   (
          t.group_id <> 0
   )
   OR
   (
     t.created_by = $user_id
   )
 )

ORDER BY c.name
SELECT t.id, t.title, t.created_by, u.username
FROM (p_tasks AS t)
LEFT JOIN p_groups AS g ON t.group_id = g.id
JOIN p_users AS u ON t.assigned_to_user_id = u.id
WHERE 
 (
   t.assigned_to_user_id = $user_id OR
   t.owner_user_id = $user_id
 )
 AND
 (
   g.priority >= '10' OR
   t.group_id = 0
 )
 AND
 (
   t.status != "approved" AND
   t.status != "closed"
 )
 AND 
 (
   (
          t.group_id <> 0
   )
   OR
   (
     t.created_by = $user_id
   )
 )

ORDER BY c.name
韶华倾负 2025-01-05 04:58:43

感谢大家!我想我知道了。

当我删除 t.group_id = 0 时,它似乎就是我想要的:-)
如果我是所有者,我会通过 owner_user_idsigned_to_user_id 选择任务。如果我不是,那么该任务是否是私有的(group_id = 0)并不重要,因为我没有足够的权限来选择/查看它。

明天我会尝试一下。

再次感谢您的帮助!!

Thanks to all! I think I get it know.

When I delete t.group_id = 0 it seems to be what I want :-)
If I am the owner I select the task by owner_user_id or assigned_to_user_id. If I am not, it doesn't matter if the task is private (group_id = 0), because I don't have enough privileges to select/view it.

I'll try that tomorrow.

Thanks again for your help!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文