php - 从 PHP Select 语句中选择数据时出现问题,获取重复数据

标签 php mysql

当我尝试内部联接或只是从 MYSQL 中的表中直接选择数据时,我遇到了获取重复数据的问题,我已检查以确保没有重复数据并且数据之间没有重复数据。我想做的就是让它 SELECT * FROM Friends INNER JOIN users ONfriends.friendsid = users.id 然后 Inner join Newsfeed 和 Comments 但是当我这样做时,它给了我重复的帖子。

关于造成此问题的原因或我如何解决此问题有什么想法吗?提前致谢!

这是我的 Select 语句的 PHP:

  $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))
{

这就是我在 PHPMYADMIN 中运行该命令时执行的操作: What it looks like after running the Select statement

这是我的新闻源表的样子:

Newsfeed Table

这是我的评论表: Comments 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
--

评论表转储:

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

然后我最终制作了一个中间表,但仍然收到重复的数据: 这是中间的表,名为 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
--

最佳答案

您在图像中看到的只是多个表的乘积。

在此示例中,帖子数据全部相同,但评论数据不同。您会看到相同的帖子数据,因为每条评论都链接到同一篇帖子,并且您提取了帖子数据和评论数据。

例如

post
1

comment  post_id
1         1
2         1

你最终会得到这个

post comment
1    1
1    2

因为每个帖子(每一行)都可以有很多评论。因此,同一帖子会出现在每一行中。这是数据库将数据显示为平面结构的唯一方法。

关于php - 从 PHP Select 语句中选择数据时出现问题,获取重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52862417/

相关文章:

php - 检测到服务器端扫描仪 "php.malware.magento-cc-stealer.069"

php - 检查当前日期是否大于存储日期的问题

php - mysql 2表根据条件更新/删除

mysql - 服务器更改后mysql的特殊字符,UTF8?

mysql - 当另一列具有相等值时对一列求和

php - 新闻聚合网站托管

php - Symfony2 : set default value from database in radio buttons choice form?

mysql - MYSQL中如何设置列的最大值?

mysql - 使用 "OR"语句时Mysql索引出现问题

javascript - 使用php将javascript函数生成的xml代码插入到mysql中并更新它