mysql - Why are variables being cut out? -
high school students trying create version of facebook our school. right stuck on joining tables in trial database before can release it.
to join tables post, users, , friends:
users user name 1 hallie 2 dylan 3 sarina 4 dominic friends user friend 1 2 1 3 1 4 2 1 3 1 4 1 2 4 4 2 3 2 2 3 posts | user | postid | post | +------+--------+------+ | 1 | 101 | tigerbook! | | 2 | 102 | pregnant. | | 1 | 103 | peeps | | 4 | 104 | giant buzz lightyears rock. | | 3 | 105 | die tucker die | | 1 | 106 | murhur de derpity derp | | 2 | 107 | banana spaghetti squid | | 4 | 108 | chicken |
we used code:
select users.user, users.name, posts.postid, posts.post, tmp.friend (select friend, user friends group friend) tmp join users on tmp.user = users.user join posts on posts.user = users.user;
and comes result of:
+------+---------+--------+-------------------------+--------+ | user | name | postid | post | friend | +------+---------+--------+-------------------------+--------+ | 1 | hallie | 101 | tigerbook! | 2 | | 1 | hallie | 101 | tigerbook! | 3 | | 1 | hallie | 101 | tigerbook! | 4 | | 2 | dylan | 102 | pregnant. | 1 | | 1 | hallie | 103 | peeps | 2 | | 1 | hallie | 103 | peeps | 3 | | 1 | hallie | 103 | peeps | 4 | | 1 | hallie | 106 | murhur de derpity derp | 2 | | 1 | hallie | 106 | murhur de derpity derp | 3 | | 1 | hallie | 106 | murhur de derpity derp | 4 | | 2 | dylan | 107 | banana spaghetti squid | 1 | +------+---------+--------+-------------------------+--------+
it cuts out posts 104, 105, , 108. posted users 3 , 4. , ignores fact user 2 friends 3 , 4, 1 shows.
why not bringing users 3 , 4? should try different code?
look subquery returns:
| friend | user | ----------------- | 1 | 2 | | 2 | 1 | | 3 | 1 | | 4 | 1 |
because of group by
, lose multiple user
values associated friend
s.
why did use subquery instead of join
ing friends
table itself?
select users.user, users.name, posts.postid, posts.post, friends.friend friends join users on friends.user = users.user join posts on posts.user = users.user;
| user | name | postid | post | friend | ------------------------------------------------------------------ | 1 | hallie | 101 | tigerbook! | 2 | | 1 | hallie | 103 | peeps | 2 | | 1 | hallie | 106 | murhur de derpity derp | 2 | | 1 | hallie | 101 | tigerbook! | 3 | | 1 | hallie | 103 | peeps | 3 | | 1 | hallie | 106 | murhur de derpity derp | 3 | | 1 | hallie | 101 | tigerbook! | 4 | | 1 | hallie | 103 | peeps | 4 | | 1 | hallie | 106 | murhur de derpity derp | 4 | | 2 | dylan | 102 | pregnant. | 1 | | 2 | dylan | 107 | banana spaghetti squid | 1 | | 2 | dylan | 102 | pregnant. | 3 | | 2 | dylan | 107 | banana spaghetti squid | 3 | | 2 | dylan | 102 | pregnant. | 4 | | 2 | dylan | 107 | banana spaghetti squid | 4 | | 3 | sarina | 105 | die tucker die | 1 | | 3 | sarina | 105 | die tucker die | 2 | | 4 | dominic | 104 | giant buzz lightyears rock. | 1 | | 4 | dominic | 108 | chicken | 1 | | 4 | dominic | 104 | giant buzz lightyears rock. | 2 | | 4 | dominic | 108 | chicken | 2 |