Jump to content

JOIN is duplicating my output


Jaynesh

Recommended Posts

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?  :confused:

Link to comment
https://forums.phpfreaks.com/topic/241537-join-is-duplicating-my-output/
Share on other sites

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.

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?)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.