0

Both queries work separately, but I need them in a single table. Im trying to save all posts by the users followers + their own posts in a single table

table following(ID, User, FollowingUser) -> saves which user follows another user

Query 1- Selects all of the posts from the current user's followers

SELECT DISTINCT PostID, posts.UserID, FollowingUser, Name, Date, Post 
FROM following, users, posts 
WHERE (User=1 AND users.UserID=FollowingUser AND posts.UserID=FollowingUser)

Query 2- Selects all of posts made by the current user

SELECT DISTINCT PostID, posts.UserID, null, Name, Date, PostID
FROM users, posts
WHERE UserID=1
Luiz Costa
  • 13
  • 2
  • 1
    Look into UNION – Jordan Soltman Dec 04 '18 at 18:04
  • If anyone's wondering, the result table is supposed to hold a post feed for the user, orderded by Date DESC – Luiz Costa Dec 04 '18 at 18:05
  • 1
    Implicit, "comma", join notation is generally out of favor, and has been for the better part of a decade or two; if you're talking about left joins, you really should convert to modern explicit join notation as the different notations generally do not play well together. ...but as Jordan S said, UNION is more likely what you need anyway. _Also, that second query doesn't get all the posts by user 1, it combines ALL posts with user 1's info._ – Uueerdo Dec 04 '18 at 18:06
  • @Uueerdo Can you support your statement, please? – Dharman Dec 04 '18 at 18:09
  • @Jordan S, yes thanks union is exactly what I wanted – Luiz Costa Dec 04 '18 at 19:25
  • @Uueerdo and thank you correcting that second query mistake for me :) – Luiz Costa Dec 04 '18 at 19:26
  • @Dharman I'll happily endorse Uueerdo's remarks. Further, the manual is quite authoritative on the matter. – Strawberry Dec 04 '18 at 19:34
  • @Strawberry I don't use the comma notation myself, for the sole reason of clarity. But I have never seen in the docs that the comma notation is discouraged. If Uueerdo is giving such statement I expect some supporting links to docs or other SO questions. – Dharman Dec 05 '18 at 15:57
  • [INNER JOIN ON vs WHERE clause](https://stackoverflow.com/a/1018825/1839439) – Dharman Dec 05 '18 at 15:59

0 Answers0