Created
December 2, 2016 01:11
-
-
Save overthemike/c4a9fa06978f58f50e01698d6d21ee0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select usernames.* from ( | |
select u.username, u.id, p.first_name, p.last_name, p.avatar, p.political_affiliation | |
from users u | |
join profiles p on p.user_id = u.id | |
join messages mf on mf.from_profile_id = p.id | |
join messages mt on mt.to_profile_id = p.id | |
where | |
mf.from_profile_id = (select p.id from users u | |
join profiles p ON u.id = p.user_id | |
join tokens t ON t.user_id = u.id | |
where t.token = ?) or | |
mf.to_profile_id = (select p.id from users u | |
join profiles p ON u.id = p.user_id | |
join tokens t ON t.user_id = u.id | |
where t.token = ?) or | |
mt.from_profile_id = (select p.id from users u | |
join profiles p ON u.id = p.user_id | |
join tokens t ON t.user_id = u.id | |
where t.token = ?) or | |
mt.to_profile_id = (select p.id from users u | |
join profiles p ON u.id = p.user_id | |
join tokens t ON t.user_id = u.id | |
where t.token = ?) | |
group by u.id | |
) as usernames | |
join users on users.id = usernames.id | |
left join tokens on tokens.user_id = users.id | |
where tokens.token != ? or ISNULL(tokens.token) |
volure
commented
Dec 2, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment