I am stuck with mysql query.Not able to proceed.
I have 2 tables where user's login time is recorded.Login time should be considered when either of the table contains entry. I want to find userwise sum of logins for a month.
I could reach till here. But not able to understand how to get sum
select table1.employeeId
, date(table1.loginTime) as date1
, date(table2.loginTime) as date2
from table1
inner join table2 on table1.employeeId=table2.employeeId
and table1.loginTime>='2017-01-01 00:00:00'
and table1.loginTime<='2017-01-31 23:59:59'
and table2.loginTime>='2017-01-01 00:00:00'
and table2.loginTime<='2017-01-31 23:59:59'
For ex : count=0 employe1 logged
on 1-Jan-2017 in table1 & table2 <- count++ (if he logs in 2 tables then only 1 count should be considered)
on 2-Jan-2017 in table1 <- count++
So for employee1 count is 2
You could do this with outer joins, but it would be needlessly complex:
select employeeId
, count(*) as loginCount
from ( select employeeId
, loginTime
from table1
where loginTime between '2017-01-01 00:00:00'
and '2017-01-31 23:59:59'
union
select employeeId
, loginTime
from table2
where loginTime between '2017-01-01 00:00:00'
and '2017-01-31 23:59:59'
( as a
group by employeeId;
Another approach:
SELECT
employeeId,
COUNT(1)+
(
SELECT COUNT(1)
FROM TABLE2 T2
WHERE MONTH(T2.loginTime) = 1
AND YEAR(T2.loginTime) = 2017
AND T2.employeeId = T1.employeeId
)
AS LOGINTIMES
FROM
TABLE1 T1
WHERE MONTH(loginTime) = 1
AND YEAR(loginTime) = 2017
GROUP BY employeeId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.