I'm having issue with getting duplicate data when trying to inner join or just plain out select data from a table in MYSQL, I have checked to make sure there is no duplicate data and there is none in between it. What I'm trying to do is make it where it will SELECT * FROM friends INNER JOIN users ON friends.friendsid = users.id And then Inner join Newsfeed and Comments But when I'm doing that it's giving me duplicate posts.
Any Ideas on Whats causing this or How I'm able to fix this? Thanks In Advance!
Here is my PHP For That Select Statement:
$stmt = $DB_con->prepare("SELECT * FROM friends INNER JOIN users ON friends.friendsid = users.id
INNER JOIN newsfeed on newsfeed.userid = friends.friendsid
INNER JOIN comments on comments.post_id = newsfeed.statusid
WHERE friends.userid = :user_id");
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
This is what it does when I run that Command in PHPMYADMIN: What it looks like after running the Select statement
Here is what my Newsfeed Table looks like:
Here is my Comments Table: Comments Table
Newsfeed Table Dump: -- -- Table structure for table newsfeed
--
CREATE TABLE `newsfeed` (
`statusid` int(255) NOT NULL,
`post_id` int(128) NOT NULL,
`userid` int(255) NOT NULL,
`status` varchar(1000) NOT NULL,
`image` varchar(255) NOT NULL,
`uploaddate` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `newsfeed`
--
INSERT INTO `newsfeed` (`statusid`, `post_id`, `userid`, `status`, `image`, `uploaddate`) VALUES
(51, 0, 0, 'KABOOM!!! :) ', '', '2018-10-10 07:13:38'),
(52, 52, 2, 'How about the refresh?!? ', '5bbe4ff2cc509.jpg', '2018-10-10 07:16:02'),
(53, 0, 3, 'Here is a new post from trail how does this look? ', '', '2018-10-16 09:31:24'),
(54, 0, 3, 'Background v3 :) ', '5bc658ba57573.jpg', '2018-10-16 09:31:38');
--
-- Indexes for dumped tables
--
Comments Table Dump:
--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`comment_id` int(128) NOT NULL,
`post_id` int(128) NOT NULL,
`comment_from` int(128) NOT NULL,
`comment` varchar(500) NOT NULL,
`comment_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `comments`
--
INSERT INTO `comments` (`comment_id`, `post_id`, `comment_from`, `comment`, `comment_date`) VALUES
(1, 52, 2, 'This is a Comment from user 2', '0000-00-00 00:00:00'),
(2, 51, 1, 'This is a comment for post id 51 from user id 1 ', '0000-00-00 00:00:00'),
(5, 52, 1, 'Post comment 3', '2018-10-16 09:18:14');
--
-- Indexes for dumped tables
--
And then I ended up making a Middle Table But am Still getting duplicate data: Here is the middle table called newsfeed_comment
--
-- Table structure for table `newsfeed_comment`
--
CREATE TABLE `newsfeed_comment` (
`id` int(128) NOT NULL,
`post_id` int(128) NOT NULL,
`comment_id` int(128) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `newsfeed_comment`
--
INSERT INTO `newsfeed_comment` (`id`, `post_id`, `comment_id`) VALUES
(1, 52, 1),
(2, 52, 2);
--
-- Indexes for dumped tables
--
What you see in your image is just the product of the multiple tables.
In this example the post data is all the same but the comment data is different. You see the same post data because each comment is linked to that same post and your pulling out both the post data and the comment data.
For example
post
1
comment post_id
1 1
2 1
You wind up with this
post comment
1 1
1 2
Because each post (each row) can have many comments. So the same post gets represented in each row. It's the only way the DB can display the data as a flat structure.