gpt4 book ai didi

MySQL 子查询从当前查询中的用户 ID 获取不同表中的用户名。如何?

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:15 24 4
gpt4 key购买 nike

以下查询是迄今为止我能够学习到的最复杂的查询。它从 tbl_user_time_log 计算登录持续时间。

SELECT USER_ID,
UTL_DTSTAMP,
UTL_TASK,
SUM(UTL_DURATION) AS UTL_DURATION_TOTAL
FROM (

SELECT A.PK_USER_TIME_LOG_ID,
A.CLIENT_ID,
A.PROJECT_ID,
A.USER_ID,
A.UTL_DTSTAMP,
/* DATE_FORMAT(A.UTL_DTSTAMP,'%H:%i:%s') AS UTL_DTSTAMP, */
A.UTL_LATITUDE,
A.UTL_LONGITUDE,
A.UTL_EVENT,
A.UTL_TASK,
/* DURATION in seconds */
TIMESTAMPDIFF(SECOND, A.UTL_DTSTAMP, B.UTL_DTSTAMP) AS UTL_DURATION
FROM tbl_user_time_log A
INNER JOIN tbl_user_time_log B
ON B.PK_USER_TIME_LOG_ID = ( A.PK_USER_TIME_LOG_ID + 1 )
WHERE A.USER_ID = '465615'
/* Between current pay period Start date and Current pay period end date */
/* First day of the week is Monday.*/

AND ( A.UTL_DTSTAMP BETWEEN '2019-02-24' AND '2019-03-04' )
/* Filter out Clock Out. */
AND A.UTL_EVENT <> 'CLOCK OUT'

/* Our derived table... */
) AS tbl_derived_1

GROUP BY USER_ID, UTL_TASK;

...返回:

query1

我如何使用相同的查询,但对于它选择的每个用户 ID,显示来自 tbl_user相应的用户名字和用户姓氏>?

tbl_user

tbl_user

最佳答案

假设您有一个表 user,其中包含通过 id 与 tbl_user_time_log 相关的名字和姓氏你可以使用连接

SELECT USER_ID,
u.firstname,
u.lastname,
UTL_DTSTAMP,
UTL_TASK,
SUM(UTL_DURATION) AS UTL_DURATION_TOTAL
FROM (

SELECT A.PK_USER_TIME_LOG_ID,
A.CLIENT_ID,
A.PROJECT_ID,
A.USER_ID,
A.UTL_DTSTAMP,
/* DATE_FORMAT(A.UTL_DTSTAMP,'%H:%i:%s') AS UTL_DTSTAMP, */
A.UTL_LATITUDE,
A.UTL_LONGITUDE,
A.UTL_EVENT,
A.UTL_TASK,
/* DURATION in seconds */
TIMESTAMPDIFF(SECOND, A.UTL_DTSTAMP, B.UTL_DTSTAMP) AS UTL_DURATION
FROM tbl_user_time_log A
INNER JOIN tbl_user_time_log B
ON B.PK_USER_TIME_LOG_ID = ( A.PK_USER_TIME_LOG_ID + 1 )
WHERE A.USER_ID = '465615'
/* Between current pay period Start date and Current pay period end date */
/* First day of the week is Monday.*/

AND ( A.UTL_DTSTAMP BETWEEN '2019-02-24' AND '2019-03-04' )
/* Filter out Clock Out. */
AND A.UTL_EVENT <> 'CLOCK OUT'

/* Our derived table... */
) AS tbl_derived_1
INNER JOIN user u ON u.id = tbl_derived_1.USER_ID

GROUP BY USER_ID, UTL_TASK;

关于MySQL 子查询从当前查询中的用户 ID 获取不同表中的用户名。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54908029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com