MySQL select unused/stale id based on timestamp period - php

I have an attendance table with simple .user and .timestamp and want to identify any .user without a timestamp since a specific time (eg 1 year ago).
I have tried several combinations of between and is not between etc., but can't seem to get the correct output (my logic and SQLs is obviously different :( )
The want to remove 'stale' users (haven't attended for a year). Once I have the data from the attendance table I will use that to delete records from other tables containing their details.

To Delete:
DELETE FROM userattendance WHERE user_id IN (SELECT user_id FROM users WHERE mytimestamp < (UNIX_TIMESTAMP() - 31557600))
It's pretty much saying DELETE data from the user attendance table where mytimestamp field is less than the current unix timestamp minus a year for any user.

Related

sql query to find event views by time period

i have the following query to return a list of events created by a user and the events total hits
SELECT view_id, user_id, event_id, date_viewed, COUNT( event_id ) AS views
FROM `tbl_event_views`
WHERE user_id =1
GROUP BY event_id
LIMIT 0 , 30
which is fine, but what i want to acheive is the number of views by days,weeks,months,years based on the date the listing was created to the current date.
could someone point me in the correct direction please?
really not sure where to start with this.
Many Thanks
Luke
You need to add a column for date created for the views. There is a timestamp for sql but theres nothing to remember when an item was created unless you add it to the database. Add that column and put the timestamp into it, then you just see how many views fall into your date range with DateCreated IN (StartDate, EndDate) or something similar

PHP MySQL calculate how many rows have the same data

Ok, well I know I can just say WHERE example='vrvrv' but that's not the case. Ok I have a rating system and whenever a page is rated the page id and the date it was rated is sent to a table in my database. Now on the homepage I want to determine top rated that day, I can easily compare dates but I'm not quite sure how to count how many rows have the same value in the project_id column. It would be amazing if someone could help me out, thanks!
You can use GROUP and COUNT to get duplicates:
SELECT count(`project_id`) AS `duplicates` FROM `table` GROUP BY `project_id` HAVING `duplicates` > 1

How do display different mysql table rows on a page, in the order they where inserted

How do social network like facebook display different content, such as photos, status, user activities onto the page, in the order it was originally posted. I have done a lot research but can't seem to find any good examples on how it's done using PHP and MYSQL.
I have include a visual example of what i mean.
I think what you mean is that the different contents are in separate tables like photos, status updates, newconnections etc. And you want the final wall to pull data from all these tables into a single wall in the same chronological order of time. So, what I will suggest is to create a new table for the wall with the following main fields :
Activity_identifier,activity_id and timestamp. Here Activity_identifier is the predefined identity of the activity such as sharing a photo (lets say 1), posting on the wall (lets say 2) etc and timestamp is the time when that activity was recorded by the user, and activity_id is the id of that activity in the corresponding table.
Everytime a new activity is created by the user, while populating the corresponding table for that activity, also record a corresponding entry in this table for wall.
Now your final query to create the wall will just have to call this new table for wall with ORDER BY TIMESTAMP DESC, then the resulting data needs to be joined with other activity tables (use Activity_identifier intelligently here) through the foreign-key activity_id to recreate the wall in the exact chronological order that the user created them.
For incremental call to the wall, maintain a timestamp of last query (lets say xtime) and every call do timestamp > xtime ... ORDER BY TIMESTAMP DESC.
Hope this helps!
Use timestamp column and keep track of each entry's insertion time in your code.
When querying data, ORDER BY timestamp column.
you can simply store a field in database which contains timestamp or creation date when specyfied content was created and while selecting it from DB just order it by mentioned creation_date :)
Use Timestamps fields in your tables (at least that's what i do) alogn with every insert (or update), with a default value of CURRENT_TIMESTAMP, this will let you sort by date (and time) of insertion with simple SQL statements
When they do the query they have an ORDER BY clause. EG:
SELECT `photos`
FROM `submissions`
ORDER BY `dateline` DESC
dateline can be stored as an integer unix time and then used with simple math to set additional parameters. EG:
$sql = "SELECT `photos`
FROM `submissions`
WHERE `dateline` >= " . (date() - 60*60*24*7) . "
ORDER BY `dateline` DESC";
This would give you only submissions from the past week.
Think of it as publishers and subscribers. Each message has a list of receivers in a junction table.
Message
------------
message_id
message_body
Message_receivers
------------
message_id
user_id
Message_photos
------------
message_id
photo
Here's how I get messages for me (user_id 1):
SELECT m.*, mp.*
FROM Message m
JOIN Message_receivers mr
ON mr.message_id = m.message_id AND mr.user_id = 1
LEFT JOIN Message_photos mp
ON mp.message_id = m.message_id
ORDER BY m.message_id DESC
You update these lists when the user posts rather than try to build the list when a user reads their news feed, since even though you may have a lot of posts, you will have more reads.
The Message_receivers records will expire and roll off. The news feed only goes back so far, though if you go to a user's page, you can see all of their posts.

Storing only the last 500 records for a user in a MySQL database

Howdie,
I'm trying writing a system that makes predictions about its users based on a test. For part of this I only want the database to store the most recent 500 values.
The current fields I am storing are
ID (Auto increment)
USERID (the users unique ID)
SCORE(an integer)
In an ideal world I would Like the system to drop the oldest row when a unique user gets over 500 records in the database. Is there an easy way to automate this in PHP without making the code really heavy?
You'll have to store a date/time field so you can determine what's over 1 month old, then it becomes as simple as:
DELETE FROM yourtable
WHERE timestampfield > (NOW() - INTERVAL 1 MONTH)
If you're not going to have any 'gaps' in the auto_incremented ID, you could do a very simple (and very failure prone):
INSERT INTO .... <--add new record
DELETE FROM yourtable WHERE (id < (INSERT_ID() - 500))
but this would be very unreliable.

Alternative solution for PHP/SQL max array size problem

I have a table bundled among 100 databases in MYSQL (i.e. 1st x rows of the table in database_1, 2nd x rows of the table in database_2, ... , last x rows of the table in database_100)
Each table has a row whenever a user visits a friend for a game.
The columns are iuin, logtime, beuin.
iuin is the user id of the visitor.
beuin is the user id of the friend who was visited.
logtime is when the visit was made.
I would like to find the # of distinct friends who were visited during a week.
There is roughly 300k distinct users who are visited per day.
However, when I extended my code to calculate for a week, I ran out of memory.
My code basically does an SQL query using SELECT DISTINCT beuin for a selected week for the table in each database. I store all the beuin in an array if it's not already stored (so I count distinct friends who were visited), and return the size of the array at the end.
FYI, I can't edit the database around such as joining all the tables in different databases into one table.
Is there any alternative ways i can do this?
Thanks
It's hard to say anything about your without the one. But I think you can solve this problem using mysql. My quick solution:
Create table - CREATE table if not exist users_ids(user_id INT NOT NULL DEAULT 0 PRIMARY KEY(UNIQUE)); in the first db
Truncate users_ids
Run 100 queries like INSERT IGNORE INTO db1.users_ids select distinct user_id from db1.table1;
Select count(*) from users_ids;

Categories