MySQL中的JOIN查询产生错误的结果

时间:2021-02-09 15:41:21

I have two tables complaints and complaints_reply in my MySQl database. Users can add complaints which are stored in complaints the complaints reply are stored in complaints_reply table. I am trying to JOIN both these table contents on a specific condition. Before I mention what I am trying to get and the problem I faced, I will explain the structure of these two tables first.

我的MySQl数据库中有两个表投诉和投诉。用户可以添加存储在投诉中的投诉,投诉回复存储在complaints_reply表中。我试图在特定条件下加入这两个表内容。在我提到我想要获得的内容以及我遇到的问题之前,我将首先解释这两个表的结构。

NB: The person who adds complaints is complaint owner & person who adds a complaint reply is complaint replier. Complaint owner can also add replies. So he can either be the complaint owner or the complaint replier. The two tables have a one-to-many relationship. A complaint can have more than one complaint reply. member_id in complaint table represents complaint owner & mem_id in complaints_reply represent complaint replier

注意:添加投诉的人是投诉所有者和添加投诉回复的人是投诉回复者。投诉所有者也可以添加回复。因此,他可以是投诉所有者或投诉回复者。这两个表有一对多的关系。投诉可以有多个投诉回复。投诉表中的member_id代表投诉所有者,投诉REP_id表示投诉回复者

DESIRED OUTPUT: Join the two tables and fetch values and show the complaint and complaint’s reply as a single result set. But the condition is kinda tricky. The last added complaint reply from the complaints_reply table should be fetched for the complaint in complaints table in such a way that the complaint owner should not be the complaint replier. I use posted_date & posted_time from complaints_reply table to fetch the last added complaint reply for a complaint & that complaint replier has to be shown in the result set.

期望的输出:加入两个表格并获取值,并将投诉和投诉的回复显示为单个结果集。但条件有点棘手。应该在投诉表中提取投诉表中最后添加的投诉回复,以使投诉所有者不应成为投诉回复者。我使用complaints_reply表中的published_date和posting_time来获取投诉的最后添加的投诉回复,并且投诉回复者必须显示在结果集中。

So, from the sample data the tables contain now, the output that I should get is:

因此,从表中包含的示例数据开始,我应该得到的输出是:

+------+---------+----------+-------------+-------------------+
| id   | title   |member_id |last_replier |last_posted_dt     |
+------+---------+----------+-------------+-------------------+
|    1 | x       | 1000     |2002         | 2015-05-2610:11:17|
|    2 | y       | 1001     |1000         | 2015-05-2710:06:16|
+------+---------+----------+-------------+-------------------+

But what I got is:

但我得到的是:

+------+---------+----------+-------------+-------------------+
| id   | title   |member_id |last_replier |last_posted_dt     |
+------+---------+----------+-------------+-------------------+
|    1 | x       | 1000     |1001         | 2015-05-2610:11:17|
|    2 | y       | 1001     |2000         | 2015-05-2710:06:16|
+------+---------+----------+-------------+-------------------+

The date is correct, but the returned complaint replier last_replier is wrong.

日期是正确的,但返回的投诉回复者last_replier是错误的。

This is my query.

这是我的查询。

SELECT com.id,
       com.title,
       com.member_id,
       last_comp_reply.last_replier,
       last_comp_reply.last_posted_dt
FROM complaints com
LEFT JOIN
  (SELECT c.id AS complaint_id,
          c.member_id AS parent_mem_id,
          cr.mem_id AS last_replier,
          max(cr.posted_dt) AS last_posted_dt
   FROM
     (SELECT cr.complaint_id,cr.mem_id,c.id,c.member_id,(CONCAT(cr.posted_date,cr.posted_time)) AS posted_dt
      FROM complaints_reply cr,
           complaints c
      WHERE cr.complaint_id=c.id
        AND cr.mem_id!=c.member_id
      GROUP BY cr.complaint_id,
               cr.mem_id,
               posted_dt)cr,
        complaints c
   WHERE cr.complaint_id=c.id
   GROUP BY cr.complaint_id,
            c.id,
            c.member_id) AS last_comp_reply ON com.id=last_comp_reply.complaint_id

Table structure for table complaints

表投诉的表结构

CREATE TABLE IF NOT EXISTS `complaints` (
  `id` int(11) NOT NULL,
  `title` varchar(500) NOT NULL,
  `member_id` int(11) NOT NULL,
  `posted_date` date NOT NULL,
  `posted_time` time NOT NULL 
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Indexes for table complaints

表投诉的索引

ALTER TABLE `complaints`
 ADD PRIMARY KEY (`id`);

AUTO_INCREMENT for table complaints

表投诉的AUTO_INCREMENT

ALTER TABLE `complaints`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;

Dumping data for table complaints

转储表投诉的数据

INSERT INTO `complaints` (`id`, `title`, `member_id`, `posted_date`, `posted_time`) VALUES
(1, 'x', 1000, '2015-05-05', '02:06:15'),
(2, 'y', 1001, '2015-05-14', '02:08:10');

Table structure for table complaints_reply

表complaints_reply的表结构

CREATE TABLE IF NOT EXISTS `complaints_reply` (
`id` int(11) NOT NULL,
  `complaint_id` int(11) NOT NULL,
  `comments` text NOT NULL,
  `mem_id` int(11) NOT NULL,
  `posted_date` date NOT NULL,
  `posted_time` time NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

Indexes for table complaints_reply

表投诉的索引

ALTER TABLE `complaints_reply`
 ADD PRIMARY KEY (`id`);

AUTO_INCREMENT for table complaints_reply

表投诉的AUTO_INCREMENT

ALTER TABLE `complaints_reply`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;

Dumping data for table complaints_reply

转储表投诉的数据

INSERT INTO `complaints_reply` (`id`, `complaint_id`, `comments`, `mem_id`, `posted_date`, `posted_time`) VALUES
(1, 1, 'reply1', 2000, '2015-05-08', '02:07:08'),
(2, 1, 'reply2', 2001, '2015-05-06', '06:05:08'),
(3, 1, 'reply3', 1000, '2015-05-14', '02:12:13'),
(4, 2, 'hola', 1000, '2015-05-27', '10:06:16'),
(5, 2, 'hello', 2000, '2015-05-04', '03:09:09'),
(6, 2, 'gracias', 1001, '2015-05-31', '06:12:18'),
(7, 1, 'reply4', 1001, '2015-01-04', '04:08:12'),
(8, 2, 'puta', 1001, '2015-06-13', '06:12:18'),
(9, 1, 'reply5', 1000, '2015-06-01', '04:08:12'),
(10, 1, 'reply next', 2002, '2015-05-26', '10:11:17');

P.S.

To give an idea about what my query is all about, I'll explain the sub query that is used to combine the tables & give result based on the condition: complaint owner should not be the complaint replier is:

为了了解我的查询的全部内容,我将解释用于组合表的子查询并根据条件给出结果:投诉所有者不应该是投诉回复者:

SELECT cr.complaint_id,
       cr.mem_id,
       c.id,
       c.member_id,
       (CONCAT(cr.posted_date,cr.posted_time)) AS posted_dt
FROM complaints_reply cr,
     complaints c
WHERE cr.complaint_id=c.id
  AND cr.mem_id!=c.member_id
GROUP BY cr.complaint_id,
         cr.mem_id,
         posted_dt

And the result for this is:

结果是:

+--------------+---------+----------+-------------+-------------------+
| complaint_id | mem_id  | id       |member_id    |     posted_dt     |
+--------------+---------+-------   +-------------+-------------------+
|    1         | 1001    | 1        |1000         | 2015-01-0404:08:12|
|    1         | 2000    | 1        |1000         | 2015-05-0802:07:08|
|    1         | 2001    | 1        |1000         | 2015-05-0606:05:08|
|    1         | 2002    | 1        |1000         | 2015-05-2610:11:17|
|    2         | 1000    | 2        |1001         | 2015-05-2710:06:16|
|    2         | 2000    | 2        |1001         | 2015-05-0403:09:09|
+--------------+---------+----------+-------------+-------------------+

member_id here represents complaint owner and mem_id represents complaint replier

member_id在这里代表投诉所有者,mem_id代表投诉回复者

The inner query gives the result based on the condition, then everything after this goes haywire. I don't know where I made mistake. The complaint replies added by complaint owner is not fetched in this table. So far so good. Is there any alternative way to get the result from here?

内部查询根据条件给出结果,然后在此之后的所有内容都变得混乱。我不知道我犯了什么错误。投诉所有者添加的投诉回复未在此表中提取。到现在为止还挺好。有没有其他方法可以从这里获得结果?

1 个解决方案

#1


This query gives the result.

此查询给出结果。

SELECT com.id AS complaint_id,
       com.member_id AS parent_mem_id,
       crep.mem_id AS last_replier,
       crl.last_posted_dt
FROM complaints com
LEFT JOIN complaints_reply crep ON com.id=crep.complaint_id
JOIN
  (SELECT cr.complaint_id,
          max(CONCAT(cr.posted_date,'_',cr.posted_time)) AS last_posted_dt
   FROM complaints_reply cr,
        complaints c
   WHERE cr.complaint_id=c.id
     AND cr.mem_id!=c.member_id
   GROUP BY cr.complaint_id)crl ON CONCAT(crep.posted_date,'_',crep.posted_time)=crl.last_posted_dt
AND crep.complaint_id=crl.complaint_id

#1


This query gives the result.

此查询给出结果。

SELECT com.id AS complaint_id,
       com.member_id AS parent_mem_id,
       crep.mem_id AS last_replier,
       crl.last_posted_dt
FROM complaints com
LEFT JOIN complaints_reply crep ON com.id=crep.complaint_id
JOIN
  (SELECT cr.complaint_id,
          max(CONCAT(cr.posted_date,'_',cr.posted_time)) AS last_posted_dt
   FROM complaints_reply cr,
        complaints c
   WHERE cr.complaint_id=c.id
     AND cr.mem_id!=c.member_id
   GROUP BY cr.complaint_id)crl ON CONCAT(crep.posted_date,'_',crep.posted_time)=crl.last_posted_dt
AND crep.complaint_id=crl.complaint_id