MySQL内连接问题选择最大行

时间:2014-07-23 14:11:33

标签: mysql inner-join

我有一些SQL下面我遇到的问题是两个引导(cosmic_leads)显示具有相同的引导阶段。我发现很难解释所以我会在底部发布一个SQL转储,这样你就可以看到表格结构等。

这是SQL: -

SELECT 
    cl.*, 
    clt.stage_name AS stagename, 
    cls.date AS moddate, 
    cls.time AS modtime, 
    cls.comments
FROM 
    cosmic_leads cl 
INNER JOIN 
    cosmic_leads_stages cls 
ON 
    cls.lead_id = cl.id
INNER JOIN (
    SELECT MAX(id) as id 
    FROM cosmic_leads_stages 
    GROUP BY site, lead_id
) clsid
ON 
    cls.id = clsid.id
INNER JOIN 
    cosmic_leads_types clt
ON 
    clt.type = cl.type AND clt.site = cl.site AND clt.stage = cls.stage
WHERE 
    cls.date >= 20140701 AND cls.date <= 20140723
GROUP BY 
    cl.id, cl.site
ORDER BY 
    cls.date DESC LIMIT 0, 10 

使用此SQL我得到以下内容: -

[
      {
        'modtime' => '145717',
        'sale' => '1',
        'name' => undef,
        'moddate' => '20140723',
        'comments' => undef,
        'username' => 'aap',
        'site' => '1',
        'handler' => undef,
        'stagename' => 'Closed - Success',
        'id' => 'lead1',
        'type' => 'Website'
      },
      {
        'modtime' => '145717',
        'sale' => '0',
        'name' => undef,
        'moddate' => '20140723',
        'comments' => undef,
        'username' => 'aap',
        'site' => '2',
        'handler' => undef,
        'stagename' => 'Closed - Success',
        'id' => 'lead1',
        'type' => 'Website'
      }
    ];

网站2上的lead1的阶段名称应为&#34;新的&#34;,任何人都可以看到问题所在吗?

由于

(下面的SQL转储)

-

- 表cosmic_leads

的表结构
CREATE TABLE IF NOT EXISTS `cosmic_leads` (
  `id` varchar(128) NOT NULL,
  `type` varchar(96) NOT NULL,
  `site` int(3) NOT NULL,
  `username` varchar(96) NOT NULL,
  `sale` varchar(48) NOT NULL DEFAULT '0',
  `name` varchar(96) DEFAULT NULL,
  `handler` varchar(96) DEFAULT NULL,
  PRIMARY KEY (`id`,`site`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-

- 转储表cosmic_leads

的数据
INSERT INTO `cosmic_leads` (`id`, `type`, `site`, `username`, `sale`, `name`, `handler`) VALUES
('lead1', 'Website', 1, 'aap', '1', NULL, NULL),
('lead1', 'Website', 2, 'aap', '0', NULL, NULL);

-

- 表cosmic_leads_stages

的表结构
CREATE TABLE IF NOT EXISTS `cosmic_leads_stages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lead_id` varchar(128) NOT NULL,
  `site` int(3) NOT NULL,
  `stage` int(3) NOT NULL,
  `date` int(8) NOT NULL,
  `time` int(6) NOT NULL,
  `comments` varchar(128) DEFAULT NULL,
  `comments_internal` varchar(128) DEFAULT NULL,
  `extra` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lead_id_site_stage` (`lead_id`,`site`,`stage`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-

- 转储表cosmic_leads_stages

的数据
INSERT INTO `cosmic_leads_stages` (`id`, `lead_id`, `site`, `stage`, `date`, `time`, `comments`, `comments_internal`, `extra`) VALUES
(8, 'lead1', 1, 1, 20140723, 145701, NULL, NULL, NULL),
(9, 'lead1', 1, 9, 20140723, 145717, NULL, NULL, NULL),
(10, 'lead1', 2, 1, 20140723, 145724, NULL, NULL, NULL);

-

- 表cosmic_leads_types

的表结构
CREATE TABLE IF NOT EXISTS `cosmic_leads_types` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `type` varchar(128) NOT NULL,
  `site` int(3) NOT NULL,
  `stage` int(3) NOT NULL,
  `stage_name` varchar(128) NOT NULL,
  `status` int(2) NOT NULL,
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`),
  UNIQUE KEY `type` (`type`,`site`,`stage`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

-

- 转储表cosmic_leads_types

的数据
INSERT INTO `cosmic_leads_types` (`id`, `type`, `site`, `stage`, `stage_name`, `status`, `amount`) VALUES
(4, 'Website', 1, 1, 'New', 1, '0.00'),
(5, 'Website', 1, 9, 'Closed - Success', 4, '0.00'),
(6, 'Website', 1, 10, 'Closed - Failed', 5, '0.00'),
(10, 'Website', 2, 1, 'New', 1, '0.00'),
(11, 'Website', 2, 9, 'Closed - Success', 4, '0.00'),
(12, 'Website', 2, 10, 'Closed - Failed', 5, '0.00');

-

- 转储表的约束

-

- 表cosmic_leads_stages

的约束
ALTER TABLE `cosmic_leads_stages`
  ADD CONSTRAINT `cosmic_leads_stages_ibfk_1` FOREIGN KEY (`lead_id`, `site`) REFERENCES `cosmic_leads` (`id`, `site`) ON DELETE CASCADE;

1 个答案:

答案 0 :(得分:2)

你有2个cosmic_leads记录,每个记录在cosmic_leads_stages上有3条记录。

这6个从cosmic_leads_stages加入到每个站点/ lead_id的最大ID,将其降低到4个记录。 2对于cosmic_leads上的每条记录: -

ID      TYPE    SITE    USERNAME    SALE    NAME    HANDLER MODDATE     MODTIME COMMENTS    STAGE
lead1   Website 2       aap          0      (null)  (null)  20140723    145724  (null)      1
lead1   Website 1       aap          1      (null)  (null)  20140723    145724  (null)      1
lead1   Website 2       aap          0      (null)  (null)  20140723    145717  (null)      9
lead1   Website 1       aap          1      (null)  (null)  20140723    145717  (null)      9

然后根据类型,网站和阶段将这4条记录加入cosmic_leads_types,并且所有4条记录都匹配,从而产生4条记录。

然后使用GROUP BY cl.id, cl.site将此值减少为每个ID /站点的1条记录。对于每个ID /网站,它已在已关闭 - 成功上找到匹配项。 GROUP BY强制它在每种情况下随机选择其中一个(它没有定义哪一个)。

正如您当前的查询一样,它正在产生预期的结果。

但是我认为要得到结果我认为您需要在 cosmic_leads cosmic_leads_stages

的连接中包含网站
SELECT 
    cl.*, 
    clt.stage_name AS stagename, 
    cls.date AS moddate, 
    cls.time AS modtime, 
    cls.comments
FROM cosmic_leads cl 
INNER JOIN cosmic_leads_stages cls 
ON cls.lead_id = cl.id
AND cls.site = cl.site
INNER JOIN 
(
    SELECT MAX(id) as id, site, lead_id
    FROM cosmic_leads_stages 
    GROUP BY site, lead_id
) clsid
ON cls.id = clsid.id
AND cls.site = clsid.site
AND cls.lead_id = clsid.lead_id
INNER JOIN cosmic_leads_types clt
ON clt.type = cl.type AND clt.site = cl.site AND clt.stage = cls.stage
WHERE cls.date >= 20140701 AND cls.date <= 20140723
ORDER BY cls.date DESC LIMIT 0, 10