last day (15 days later) » 

22:20
0
A: SQL Query - What to do

JennEssentially what you are looking for boils down to you wanting the username and location with the most recent time stamp. So, you want to ignore all the records whose last activity is not the greatest. Select sum(1) as testVar From session s Where location='chat.php' and datediff(minute, s.last...

Unfortunately my previous comment still stands. I am very confused... My table name is session. After the query is returned I want to get the amount of rows returned into a variable called: testVar. I then want to minus one from testVar to give me my final result. Can someone help me by giving me the full code? Please submit as an answer so I can mark as correct if it is :)
@user3474238 I don't quite understand what testVar is. You want the number of rows (minus one) for the user?
No, I want to query the database for all the rows where location='chat.php'. I only want one row per userid which is determined by whichever is the most recent submission. Then I also don't want any that are older than 15 minutes. Finally I want to count the number of rows returned and put them into a variable called testVar
@user3474238 you should add this information into your question, it isn't clear right now.
Added it. Are you able to help me?
22:20
So in reality, you are looking for the number of unique users that were in chat.php in the past 15 minutes, is that correct?
hi there.
Yes that's correct.
okay, this is suddenly a lot less difficult of a problem
:D
Table name: session
22:29
since you are trying to get the number of unique users, you can completely ignore the last activity time (except for the whole within the last 15 minutes part)
Okay, so let's look at the different pieces
Select
what information do you want?
Also a big plus would be then changing the userid to the username which can be found in the user table
location, lastactivity, userid
well, you can't have it both ways
22:31
if you want a count of the number of users in chat.php, you cannot select the username
I'd say counting is fine but if we can just show the usernames it would be better
okay, so the count doesn't matter as much as knowing who is in the chat
so to find all the people in the chat, you can do
Select distinct userid
we will talk about switching it to the username in a little bit
22:33
So we already have:
Select distinct userid
From session s
session S? what does the S mean?
the s is called an alias
it's nice to alias your tables because from now on we can just refer to the table as s
it's like having a nickname
so now we want to make sure that we only have the users in the one location
which means we need a where clause
ok
yep
WHERE location='chat.php;
'*
22:35
exactly
So far:
SELECT distinct userid FROM session s WHERE location='chat.php'
if you had two tables in this query, you would want to use the alias here and say s.location (instead of just location)
ah ok
Should distinct b in caps?
it can be
sql ignores case
22:37
so next we need to make sure that the last activity was in the past 15 minutes
this part is a little bit complicated
so first we need to know what the time is right now
ok
the timezone on the thingy is GMT+1
getdate() is a built in function in sql. it is a date time, so it returns the current date and time
22:38
I think getdate() handles the timezone
but I'm actually not sure on that
anyway, we'll use that for now. you may need to change it later
so the next function is datediff
this function allows you to find the difference between two date times and you get to specify what interval you want
so you can find the number of years two date differ
or the number of minutes
22:41
the format is datediff(minute, s.lastactivity, getdate())
this will return the number of minutes between these two date times
if we were to switch it to datediff(minute, getdate, s.lastactivity))
it would still return the difference in minutes, but the result would be negative
so this is going to go into your where clause as well
we want:
Where location='chat.php'
and datediff(minute, s.lastactivity, getdate()) < 15
Ok. One minute
22:43
k
Should I try that code?
yes. don't forget the semi-colon at the end
another thing to note, you had
SELECT * FROM session WHERE location='chat.php' GROUP BY userid
you can use group by instead of distinct
they do the same thing
but all the columns in your select must be in your group by
I actually think group by is more useful
the result is null (nothing) so the testVar is showiing blank instead of 0 at the least..
22:48
why don't you try just this query and see if it gives you the right users
An error occurred while attempting to execute your query. The following information was returned.
error number: 1582
error desc: Incorrect parameter count in the call to native function 'datediff'
shoot. hold on
whoops
try timediff(s.lastactivity, getdate())
ok
function getdate does not exist
22:53
try now() instead
sorry, I'm used to sql server
Where do I do that?
instead of getdate()
wait
is lastactivity just a time
or is it a datetime
lemme see
I am going to need to get going, so this is what I am going to suggest. try selecting the timediff(s.lastactivity, now()) to see what it returns. it may return the difference in 00:00:00 format, so you may need to use that format when you say < 15. also, try looking up joins
the join will allow you to squish sessions and your table with usernames together so that you can select the username instead of the userid
this is where the alias will come in
22:59
SELECT distinct u.username
FROM session s
JOIN usernames u
on s.userid = u.userid
WHERE location='chat.php'
and timediff(s.lastactivity, now()) < 15;
it will look something like this
but you will have to figure out the details on your own. good luck!
tried it with now
And it showed 0 results but I know there is at-least one
well try Select timediff(s.lastactivity, now()) from sessions and see what you get
ok
it also returned 0

last day (15 days later) »