A SQL Join
March 16, 2006 3:03 PM
Subscribe
How do I manage this SQL join?
I have two tables: USERS and LOGGED_IN_USERS created as follows (non-relevant columns removed)
CREATE TABLE Users
(
user_id INTEGER AUTO_INCREMENT PRIMARY KEY,
remember TINYINT(1) NOT NULL
)
CREATE TABLE LoggedInUsers
(
user_id INTEGER PRIMARY KEY,
session_id VARCHAR(255) UNIQUE NOT NULL,
last_access DATETIME NOT NULL,
FOREIGN KEY (user_id) REFERENCES Users (user_id)
)
What I want to do is write a query that deletes rows from the LoggedInUsers whose last_access is more than some variable $interval hours old and whose value in the Users.remember column is 0.
This query works fine to delete old logins
DELETE FROM LoggedInUsers
WHERE last_access <= DATE_SUB(NOW(), INTERVAL $interval)
Now I want to add the condition that the DELETE only happen if the 'remember' value in table Users is 0 for the row with the same user_id.
posted by nonmyopicdave to computers & internet (13 comments total)
posted by Bezbozhnik at 3:05 PM on March 16, 2006