Jaynesh Posted July 9, 2011 Share Posted July 9, 2011 Hello I have two tables Posts(post_id,user_id, post) vote (vote_id, up, down) Let's say I have 5 rows in post and 2 rows in vote. If I execute this sql query, it will output all the posts that match the users id. SELECT DISTINCT Posts.post, vote.user_id FROM Posts, vote WHERE Posts.user_id = $user"; If I execute this sql query, with a small addition(in red) SELECT DISTINCT Posts.post, vote.user_id, vote.vote_id FROM Posts, vote WHERE Posts.user_id = $user"; It will output all the posts that match the users id multiplied by how many rows there are in the vote table. WHY IS THIS HAPPENING? Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/ Share on other sites More sharing options...
Psycho Posted July 10, 2011 Share Posted July 10, 2011 I'm not sure what you are trying to do, but I'm pretty sure you are doing it wrong. I don't see any relationship between your Posts table and your vote table. When joining two tables it will join every record from one table on every record from the other table - doing exactly what you described. Is the vote_id supposed to be the foreign key for the post ID? Your table structure just doesn't make any sense. Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1240699 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 Hello my vote_id assigns an incremented number to each row. I don't want that column to be joined however I want to output that column in my script. How do I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1240905 Share on other sites More sharing options...
phpmady Posted July 10, 2011 Share Posted July 10, 2011 Hi, Please Post your question in MYSQL section Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1240911 Share on other sites More sharing options...
Psycho Posted July 10, 2011 Share Posted July 10, 2011 my vote_id assigns an incremented number to each row. I don't want that column to be joined however I want to output that column in my script. If there is no relationship between the posts table and the votes table then you shouldn't try and pull results from both tables in one query. Although, I would assume there SHOULD be a relationship between the tables (i.e. the users vote on the posts?) Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1240961 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 Hello I want to output the vote_id and use it in an if statement with another output from the query. What is my alternative? Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1240964 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 sorry, I just realized that I am an idiot. Thank you for your efforts. Quote Link to comment https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/#findComment-1241004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.