Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get users Last Login Data 1

Status
Not open for further replies.

Marclem

Technical User
Aug 5, 2003
87
US
Hi Everyone,

I had this thread on SQL Server forum and was told since I am using MySQL to ask for this request here:


I am a newbie getting started with SQL and did some search online but so far have not been able to solve and gather the data I need.

I need to get all users last login data so I may then delete user accounts that not logged in for a long time as I have reached the max license usage for this system.

I have found the Opinio SQL table and drilled down to table OPS_UserAttribute where I can see column named AttributeName which when I run a search on this table using MySQL the data results in last login instead of showing me the actual date of last login (below a screenshot of data I am looking for)
Last_Login_data_vczh5i.png
.

I ran below SQL command but all I got was data as 0 or 1:
sql_CMD_f7yx9h.png



Thank you in advance for your support!
 
Just a guess, but is the select statement carrying out a comparison and returning 0 or 1 depending on whether the comparison is true or false?

Do you need to also get some other fields?
 
When I run below Select statement to gather all column data from the OPS_UserAttribute table I can see the "last login" - I think what I need is to decipher the "StringValue" and\or the LongValue, BigTextValue real data as it shows Null on some but I can see data on LongValue column but that shows numeric numbers instead of actual Date data...

OPS_Attribute_bdde8o.png



Correct I need to match this "Last Login" info with the users which I believe is under the table OPS_User which shows column "UserId" on both tables

OPS_User_zaxfgm.png
 
The long string is a unix timestamp. The first row is for May 25, 2021. Convert here...
Code:
SELECT *, FROM_UNIXTIME(`LongValue`) AS `timestamp` FROM opinio.OPS_UserAttribute WHERE `AttributeName` = 'last login' ORDER BY `LongValue` ASC

This shows what you've queried before (*) but adds a column at the end with a readable timestamp (FROM_UNIXTIME(`LongValue`)). It also orders the result so the oldest logins appear first (ASCending).

Caution should be exercised here. Limit your SQL activity to SELECT; not DELETE or UPDATE. Without fully understanding the structure of this system, removing accounts in SQL may wreck the application, especially if other data is related to these accounts in other tables. Use this only to list users, but remove the users within the web application.
 
Thank you so much for the support!

I ran below Code and I got many 1969 year date since "LongValue" was "0" which means user never logged in so no timestamp (that's ok)... then I changed order by to "DESC" and I can see "NULL" value under timestamp. As an example for 1 userid 10553 system shows date of last login as: May 20, 2021 2:10 PM but comes up as "Null" value running below code (looks like Code was not able to convert the "LongValue":
Code:
SELECT *, FROM_UNIXTIME(`LongValue`) AS `timestamp` FROM opinio.OPS_UserAttribute WHERE `AttributeName` = 'last login' ORDER BY `LongValue` DESC


Unixtime_dc0czt.png
 
That null for `timestamp` is very odd. I cannot explain that.
 
How can I add to the Code the "Login" data coming from the "opinio_OPS_User" table as both "opinio_OPS_User" and "opinio_OPS_UserAttribute" tables have the unique column match "Userid"

Code:
select Login, UserId from opinio.OPS_User
 
samjim,

Did a research on FROM_UNIXTIME showing null values and I got it working, basically because unix time is in seconds and data shows stored it in milliseconds requiring to divide by 1000:

Code:
SELECT *, FROM_UNIXTIME(`LongValue` /1000) AS `timestamp` FROM opinio.OPS_UserAttribute WHERE `AttributeName` = 'last login' ORDER BY `LongValue` DESC


UNIXTIME-1000_no41uv.png
 
Marclem said:
because unix time is in seconds and data shows stored it in milliseconds requiring to divide by 1000

Whoops. I was not paying attention to the length of those strings. Well done!
 
>How can I add to the Code the "Login" data coming from the "opinio_OPS_User" table
>as both "opinio_OPS_User" and "opinio_OPS_UserAttribute" tables have the unique
>column match "Userid"

[tt]...
From opinio_OPS_UserAttribute, opinio_OPS_User
WHERE opinio_OPS_UserAttribute.Userid = opinio_OPS_User.Userid[/tt]

or use aliases for your tables:
[tt]...
From opinio_OPS_UserAttribute A, opinio_OPS_User U
WHERE A.Userid = U.Userid[/tt]

or
[tt]SELECT *, FROM_UNIXTIME(`LongValue` /1000) AS `timestamp`,[blue]
(Select Login From opinio_OPS_User Where UserId = opinio_OPS_UserAttribute.UserId) AS TheUser[/blue]
FROM opinio_OPS_UserAttribute
WHERE `AttributeName` = 'last login'
ORDER BY `LongValue` DESC
[/tt]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Thank you so much Andy!

I went ahead and added another field from OPS_user table:

Code:
SELECT FROM_UNIXTIME(`LongValue` /1000) AS `timestamp`,
(Select Login From opinio.OPS_User Where UserId = opinio.OPS_UserAttribute.UserId) AS BOPID,
(Select UserName From opinio.OPS_User Where UserId = opinio.OPS_UserAttribute.UserId) AS UserName
FROM opinio.OPS_UserAttribute
WHERE `AttributeName` = 'last login'
ORDER BY `LongValue` DESC


Thank you as well spamjim!

Not sure if it is possible to do, during the sql code, but when I export the data into a csv file the "timestamp" value gets gibberish which I have to manually do a custom "Format Cell" on the column and set as "yyyy-mm-dd" to get data correctly in excel as "2016-04-04
 
You may want to Google: "mysql convert timestamp to date" :)

Also, you may get a better performance doing this instead:

[pre]SELECT FROM_UNIXTIME(A.LongValue /1000) AS `timestamp`,
U.Login AS BOPID, U.UserName
FROM opinio_OPS_UserAttribute A, opinio_OPS_User U
WHERE A.UserId = U.UserId
And A.AttributeName = 'last login'
ORDER BY LongValue DESC [/pre]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Thanks Andy,

I did a research as you suggested and ended as (will as well do some edit as you suggested to have a better performance):

Code:
Select DATE_FORMAT(FROM_UNIXTIME(`LongValue` /1000), '%m/%d/%Y') AS `timestamp`,
(Select Login From opinio.OPS_User Where UserId = opinio.OPS_UserAttribute.UserId) AS BOPID,
(Select UserName From opinio.OPS_User Where UserId = opinio.OPS_UserAttribute.UserId) AS UserName
FROM opinio.OPS_UserAttribute
WHERE `AttributeName` = 'last login'
ORDER BY `LongValue` DESC
 
Ended with below code:

Code:
SELECT DATE_FORMAT(FROM_UNIXTIME(`LongValue` /1000), '%m/%d/%Y') AS `timestamp`,
U.Login AS BOPID, U.UserName
FROM opinio.OPS_UserAttribute A, opinio.OPS_User U
WHERE A.UserId        = U.UserId
  And A.AttributeName = 'last login'
ORDER BY LongValue DESC

Thank you everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top