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

时间:2015-05-10 20:04:35

标签: mysql join left-join inner-join

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

NB :添加投诉的人是投诉所有者&添加投诉回复的人是投诉回复者。投诉所有者也可以添加回复。因此,他可以是投诉所有者或投诉回复者。这两个表有一对多的关系。投诉可以有多个投诉回复。 member_id表格中的complaint表示投诉所有者& mem_id中的complaints_reply代表投诉回复

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

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

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

但我得到的是:

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

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

这是我的查询

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

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 ;

表格complaints

的索引
ALTER TABLE `complaints`
 ADD PRIMARY KEY (`id`);

complaints

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

转储表格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');

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 ;

表格complaints_reply

的索引
ALTER TABLE `complaints_reply`
 ADD PRIMARY KEY (`id`);

complaints_reply

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

转储表格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。

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

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

结果是:

+--------------+---------+----------+-------------+-------------------+
| 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此处代表投诉所有者,mem_id代表投诉回复

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

1 个答案:

答案 0 :(得分:0)

此查询提供结果。

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