具有来自同一行的值的子查询

发布于 2024-12-04 04:24:48 字数 1597 浏览 0 评论 0原文

我有一个关于 MySQL 的小问题 我有包含以下列的数据库(Arr_Times) mysql_id 姓名 ARR_DT(到达日期时间) DC_DT(出发日期时间)

Mysql_id | Name           | ARR_DT              | DC_DT
1        |Doe, Jane       | 2011-08-01 00:02:00 | 2011-08-01 13:32:00
2        |Doe, John       | 2011-08-01 00:23:00 | 2011-08-01 18:32:00
3        |Smith, Peter    | 2011-08-01 00:12:00 | 2011-08-01 08:38:00
4        |Unknown, Jane   | 2011-08-01 13:03:00 | 2011-08-01 14:02:00
5        |Day, Doris      | 2011-08-01 10:10:00 | 2011-08-01 13:32:00
6        |Bergman, Ingrid | 2011-08-01 11:35:00 | 2011-08-01 13:59:00
7        |Jones, Tom      | 2011-08-01 00:12:00 | 2011-08-01 22:49:00
8        |Schubert, Mark  | 2011-08-01 18:45:00 | 2011-09-01 02:22:00
9        |Doe, Jane       | 2011-08-01 23:04:00 | 2011-010-01 03:32:00

一般大约有。每天 150-220 排,出发时间最多可以晚于到达时间 48 小时。 我试图找出在任何给定时间内有多少人在部门

例如,当人员到达时自动计算每行的部门人员,规则如下:计算 ARR_DT <= current_row's ARR_DT AND DC_DT > 的每个人current_row 的 ARR_DT 获得结果,例如导出到 Excel 数据透视表。

mysql_id | Name | ARR_DT_unix | DC_DT_unix | Number_In_department

但这会遍历整个数据库的每一行,并且非常消耗 CPU。对于这个问题有没有更好的解决办法。

以下是我目前正在尝试的方法,但结果不正确

SELECT
  T1.mysql_id,
  T1.Name, 
  FROM_UNIXTIME(T1.ARR_DT_unix) AS ARR_DT,
  FROM_UNIXTIME(T1.DC_DT_unix) AS DC_DT,
  (SELECT count(T2.mysql_id) FROM dates T2 WHERE T1.ARR_DT_unix <= T2.ARR_DT_unix AND T1.DC_DT_unix > T2.ARR_DT_unix ) AS Person_Count 
FROM 
  dates T1
INNER JOIN 
 dates T2
ON T1.mysql_id = T2.mysql_id
ORDER BY T1.ARR_DT_unix;

对于此问题,最佳选择是什么?

谢谢, 马尔科

I have a small problem with MySQL
I have database (Arr_Times) with following columns
Mysql_id
Name
ARR_DT (Arrival datetime)
DC_DT (Departure datetime)

Mysql_id | Name           | ARR_DT              | DC_DT
1        |Doe, Jane       | 2011-08-01 00:02:00 | 2011-08-01 13:32:00
2        |Doe, John       | 2011-08-01 00:23:00 | 2011-08-01 18:32:00
3        |Smith, Peter    | 2011-08-01 00:12:00 | 2011-08-01 08:38:00
4        |Unknown, Jane   | 2011-08-01 13:03:00 | 2011-08-01 14:02:00
5        |Day, Doris      | 2011-08-01 10:10:00 | 2011-08-01 13:32:00
6        |Bergman, Ingrid | 2011-08-01 11:35:00 | 2011-08-01 13:59:00
7        |Jones, Tom      | 2011-08-01 00:12:00 | 2011-08-01 22:49:00
8        |Schubert, Mark  | 2011-08-01 18:45:00 | 2011-09-01 02:22:00
9        |Doe, Jane       | 2011-08-01 23:04:00 | 2011-010-01 03:32:00

Generally there is approx. 150-220 rows for each day and departure time can be up to 48 hours later from arrival time.
I’m trying to find out how many persons are in department in any given time

For example automatically calculating persons in department for each row when person arrives with rule: count everyone whom ARR_DT <= current_row’s ARR_DT AND DC_DT > current_row’s ARR_DT
To have a result, for example to be exported to excel pivot table.

mysql_id | Name | ARR_DT_unix | DC_DT_unix | Number_In_department

But this goes through whole database for each row and is highly CPU consuming. Is there any better solution for this issue.

Following is what I am currently attempting, but I am having incorrect results

SELECT
  T1.mysql_id,
  T1.Name, 
  FROM_UNIXTIME(T1.ARR_DT_unix) AS ARR_DT,
  FROM_UNIXTIME(T1.DC_DT_unix) AS DC_DT,
  (SELECT count(T2.mysql_id) FROM dates T2 WHERE T1.ARR_DT_unix <= T2.ARR_DT_unix AND T1.DC_DT_unix > T2.ARR_DT_unix ) AS Person_Count 
FROM 
  dates T1
INNER JOIN 
 dates T2
ON T1.mysql_id = T2.mysql_id
ORDER BY T1.ARR_DT_unix;

What would be the best option for this problem?

Thanks,
Marko

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

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

发布评论

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

评论(1

柠檬心 2024-12-11 04:24:48

您可以使用

 T1.mysql_id,

in where 子句来限制子查询中的搜索,这可能会解决您的问题

You can use

 T1.mysql_id,

in where clause to restrict search in your sub query this might solve your problem

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