-1
SELECT * 
FROM `Events` 
WHERE `UserID` IN (SELECT `BeingFollowed` FROM `Followers` WHERE `Follower` = us)

Ok so above is my query and i'm trying to get all the events made by my followers and the us variable is a parameter in a stored routine function which is my UserID but i'm not following myself but i want to get my events also and every event has a UserID and I wanted to get all of my followers events as well as the ones i have all within the same query

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Andrew Edwards
  • 1,005
  • 1
  • 9
  • 24

1 Answers1

2

That's what OR does:

SELECT * 
FROM `Events` 
WHERE `UserID` IN (SELECT `BeingFollowed` FROM `Followers` WHERE `Follower` = us)
OR `UserID` = us

You could also add yourself to the subquery, but that's probably slower:

SELECT * 
FROM `Events` 
WHERE `UserID` IN (
  SELECT `BeingFollowed` FROM `Followers` WHERE `Follower` = us
  UNION ALL
  SELECT us
)
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • @DrewPierce: Right. So MySQL still doesn't have that CBO? I keep forgetting... In this case, it won't help though, as `Followers` models a `M:N` relationship. – Lukas Eder Jun 04 '15 at 23:24
  • @Lukas Eder Thanks buddy the UNION works best when i have additional conditional clauses after my IN clause – Andrew Edwards Jun 05 '15 at 00:15