You do not want a JOIN, you want a UNION. Look
SELECT users.user_id, 'users'
FROM users
WHERE users.name='MikeBOSS'
UNION
SELECT users_archive.user_id, 'archive'
FROM users_archive
WHERE users_archive.name='MikeBOSS';
Question
I have a query that I would like to run but is not returning the expected results.
So my tables are like this
users (has two columns)
user_id,name
users_archive (has the same two columns)
user_id,name
I want to basically run a query that lists user_id from the respective table where the username matches what I'm searching for
For my example I have a user called MikeBOSS in users_archive with an user_id of 123 (there is no MikeBOSS in users table)
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
but that returns no results
SELECT users.user_id, users_archive.user_id
FROM users, users_archive
WHERE (users.name='MikeBOSS' OR users_archive.name='MikeBOSS');
That returns a bunch of results from the users table that are incorrect.
Could someone maybe point me in the correct direction?
Solution
You do not want a JOIN, you want a UNION. Look
SELECT users.user_id, 'users'
FROM users
WHERE users.name='MikeBOSS'
UNION
SELECT users_archive.user_id, 'archive'
FROM users_archive
WHERE users_archive.name='MikeBOSS';
OTHER TIPS
A join condition normally links two tables. Yours does not:
ON users_archive.name='MikeBOSS'
A join condition that does link the two tables might look something like:
ON users.name = users_archive.name
If you wonder about the number of rows this returns, check each table individually. Is there even a row with name = 'MikeBoss'
in the users_archive
table?
Change
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
To
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.name = users_archive.name WHERE users.name='MikeBOSS';
May be this could work if you have no relation between 2 tables. Using left join gives you record from one table even if its not present in other.
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.userid = users_archive.userid and users.name = users_archive.name
WHERE users.name='MikeBOSS';