简体   繁体   中英

Subquery in Mysql

I'm trying to count users signin in DB, the problem is to count uniq signin by date. The problem, user can have a multiple signin a day.

For example table Student StudentID

Hour TypeID = StudentID, Date, Time 123, 2014-11-11, 00:11:00 123, 2014-11-11, 00:15:00 123, 2014-11-11, 00:16:00 etc.

I'm trying to use this query...

    select count(*) from (
select '1' from Students s LEFT JOIN Hours h on s.StudentID = h.TypeID
where s.StudentID = stud.StudentID
group by h.Date) tmp, Student stud
where s.LastName = 'TestUser'

But subquery shows an error: Unknown column stud.StudentID

Error is very clear, column StudentID doesn't exists in "stud" alias. You define the alias as "s". Maybe something like this:

select count(*) from (
    select '1' from Students s 
    LEFT JOIN Hours h on s.StudentID = h.TypeID
    where s.StudentID = h.StudentID
    group by h.Date
) tmp

But this won't take effect since you have not selected anything.

, Student s
where s.LastName = 'TestUser'

But I think is still not functionally. What are you trying to do?

try this one :

Select h.Date, Count(Distinct TypeID) as tot
From [Hours] as h
Left Join Students s
    On s.StudentID = h.TypeID
where s.LastName = 'TestUser'
Group By h.Date

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM