mySQL - 将 id 传递给嵌套查询

发布于 2024-12-08 12:34:35 字数 876 浏览 0 评论 0原文

我正在尝试获取同一个表中按用户分组的两个不同列的总和。问题是,我必须找到的总和之一需要一些额外的参数。

表 1:

userid, fname, lname

表 2:

assigned_to (is = to userid from other table), 
est_hours (est = Estimate hours),
act_hours (act = Actual Hours), 
completed_date 

我只想要已标记为已完成的条目中的 est_hours(通过在completed_date 列中包含一个值。如果completed_date 为空,act_hours 将为空)

我的查询的问题是我没有得到正确的结果act_hours 和 est_hours 的总和值。我认为这与嵌套查询不匹配正确的用户 ID 有关。 查询:

SELECT userid, 
       fname, 
       lname, 
       SUM(act_hours)+(SELECT SUM(est_hours) 
                       from Table2 
                       WHERE completed_date IS NULL) AS total_hours 
FROM TABLE2, 
TABLE1 
WHERE TABLE1.userid = TABLE2.assigned_to 
GROUP BY userid;

我知道我需要以某种方式将用户 ID 从外部查询传递到内部查询,以便 SUM(est_hours) 仅获取该用户的总和。

我一直在兜圈子,我的大脑很痛,所以任何帮助都是值得赞赏的!

I am trying to get the sum of the sums of two different columns from the same table, grouped by user. The problem is, one of sums I have to find, requires some additional paramters.

Table 1:

userid, fname, lname

Table 2:

assigned_to (is = to userid from other table), 
est_hours (est = Estimate hours),
act_hours (act = Actual Hours), 
completed_date 

I only want the est_hours from entries that have been marked as completed (by containing a value in the column completed_date. act_hours will be blank if completed_date is blank)

The problem with my query is I am not getting the correct values for the sum of the act_hours and est_hours. I think it has to do with the nested query not matching up the right userid.
Query:

SELECT userid, 
       fname, 
       lname, 
       SUM(act_hours)+(SELECT SUM(est_hours) 
                       from Table2 
                       WHERE completed_date IS NULL) AS total_hours 
FROM TABLE2, 
TABLE1 
WHERE TABLE1.userid = TABLE2.assigned_to 
GROUP BY userid;

I know I need to somehow pass the userid from the OUTER query, into the inner query, so that the SUM(est_hours) is only getting the sum for that user.

I keep going in circles and my brain hurts, so any help is appreciated!!

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

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

发布评论

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

评论(2

将军与妓 2024-12-15 12:34:35

不要使用隐式连接语法,这是一种反模式

使用显式连接代替:

SELECT 
  t1.userid, 
  t1.fname, 
  t1.lname, 
  SUM(t2.act_hours)+(SELECT SUM(t3.est_hours) 
                     FROM Table2 t3
                     WHERE t3.completed_date IS NULL
                       AND t3.assigned_to = t1.userid) AS total_hours 
FROM table2 t2
INNER JOIN table1 t1 ON (t1.userid = t2.assigned_to)
GROUP BY userid;

解决您头痛的方法是使用表别名,然后您可以引用同一个表的不同实例。
我强烈建议始终使用表别名重新修复所有字段,这样就不会混淆字段来自哪个表(哪个表的实例)。
这也将缩短您的行并使您的查询更具可读性。

Do not use implicit join syntax, it's an anti-pattern

Use explicit joins instead:

SELECT 
  t1.userid, 
  t1.fname, 
  t1.lname, 
  SUM(t2.act_hours)+(SELECT SUM(t3.est_hours) 
                     FROM Table2 t3
                     WHERE t3.completed_date IS NULL
                       AND t3.assigned_to = t1.userid) AS total_hours 
FROM table2 t2
INNER JOIN table1 t1 ON (t1.userid = t2.assigned_to)
GROUP BY userid;

The solution to your headache is to use table aliases, then you can refer to different instances of the same table.
I strongly advice to always refix all fields with the table alias, so there can be no confusion which (instance of which) table the field comes from.
This will also shorten your lines and make your query more readable.

開玄 2024-12-15 12:34:35

这在 SQL Server 中有效,在 MYSQL 中尝试一下,可能有效:

SELECT userid, 
       fname, 
       lname, 
       (SELECT SUM(est_hours)+SUM(act_hours) 
                       from Table2 
                       WHERE completed_date IS NULL and TABLE2.assigned_to=TABLE1.userid) AS total_hours 
FROM 
TABLE1 

好的,恢复到我原来的查询...

SELECT userid, 
       fname, 
       lname, 
       SUM(act_hours)+(SELECT SUM(est_hours) 
                       from Table2 
                       WHERE completed_date IS NULL 
                       AND Table2.assigned_to=Table1.userid) 
       AS total_hours 
FROM TABLE2, 
TABLE1 
WHERE TABLE1.userid = TABLE2.assigned_to 
GROUP BY userid;

This is valid in SQL Server, try it in MYSQL, may work:

SELECT userid, 
       fname, 
       lname, 
       (SELECT SUM(est_hours)+SUM(act_hours) 
                       from Table2 
                       WHERE completed_date IS NULL and TABLE2.assigned_to=TABLE1.userid) AS total_hours 
FROM 
TABLE1 

OKAY, reverting back to my original query...

SELECT userid, 
       fname, 
       lname, 
       SUM(act_hours)+(SELECT SUM(est_hours) 
                       from Table2 
                       WHERE completed_date IS NULL 
                       AND Table2.assigned_to=Table1.userid) 
       AS total_hours 
FROM TABLE2, 
TABLE1 
WHERE TABLE1.userid = TABLE2.assigned_to 
GROUP BY userid;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文