I have the following two tables,
CREATE TABLE logins (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
user_id_1 INT NOT NULL,
user_id_2 INT DEFAULT 0,
user_id_3 INT DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=MyISAM;
CREATE TABLE user_data (
user_id NOT NULL,
day DATE NOT NULL,
PRIMARY KEY (`user_id, `day`)
) ENGINE=MyISAM;
This schema could use a refactor, but I've inherited it and have to write a query now that does a JOIN
with both logins
and user_data
. I need to select all the rows in user_data
that have a > 0
value for one of the three user_id_?
keys.
I'm not entirely sure how to compile this query, was thinking something along the lines of:
SELECT logins.user_id_1, logins.user_id_2, logins.user_id_3, user_data.day,
FROM logins
INNER JOIN user_data
ON (logins.user_id_1 = user_data.user_id OR ??)
What's the best way to query for this where I will retrieve up to 3 rows, one for each user_id_?