MySQL 复杂半连接,无需分组依据



摘要

我正在寻找一个半连接(ish)查询,该查询选择多个客户并从其他表中联接他们的最新数据。

稍后,我希望直接将条件附加到查询的末尾:WHERE c.id IN (1,2,3)

问题

据我所知,我的要求排除了GROUP BY

SELECT * FROM customer c
LEFT JOIN customer_address ca ON ca.customer_id = c.id
GROUP BY c.id
# PROBLEM: Cannot append conditions *after* GROUP BY!

对于大多数基于子查询的尝试,我的问题是一样的。

作为另一个挑战,我不能严格使用半联接,因为我允许至少两种类型的电话号码(移动和固定电话),它们来自同一个表。因此,从电话表中,我可能会为每个客户加入多个记录,即这不再是半连接。我目前下面的解决方案说明了这一点。

问题

  • 底部的EXPLAIN结果对我来说看起来很有表现。我说的对吗?每个子查询是否只执行一次更新:似乎DEPENDENT SUBQUERY对外部查询中的每一行执行一次。如果我们能避免这种情况,那就太好了。
  • 对于我正在做的事情,有更好的解决方案吗?

DDL

DROP TABLE IF EXISTS customer;
CREATE TABLE `customer` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);
DROP TABLE IF EXISTS customer_address;
CREATE TABLE `customer_address` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` bigint(20) unsigned NOT NULL,
  `street` varchar(85) DEFAULT NULL,
  `house_number` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
  );
DROP TABLE IF EXISTS customer_phone; 
CREATE TABLE `customer_phone` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` bigint(20) unsigned NOT NULL,
  `phone` varchar(32) DEFAULT NULL,
  `type` tinyint(3) unsigned NOT NULL COMMENT '1=mobile,2=landline',
  PRIMARY KEY (`id`)
  );
insert ignore customer values (1);
insert ignore customer_address values (1, 1, "OldStreet", 1),(2, 1, "NewStreet", 1);
insert ignore customer_phone values (1, 1, "12345-M", 1),(2, 1, "12345-L-Old", 2),(3, 1, "12345-L-New", 2);
SELECT * FROM customer;
+----+
| id |
+----+
|  1 |
+----+
SELECT * FROM customer_address;
+----+-------------+-----------+--------------+
| id | customer_id | street    | house_number |
+----+-------------+-----------+--------------+
|  1 |           1 | OldStreet |            1 |
|  2 |           1 | NewStreet |            1 |
+----+-------------+-----------+--------------+
SELECT * FROM customer_phone;
+----+-------------+-------------+------+
| id | customer_id | phone       | type |
+----+-------------+-------------+------+
|  1 |           1 | 12345-M     |    1 |
|  2 |           1 | 12345-L-Old |    2 |
|  3 |           1 | 12345-L-New |    2 |
+----+-------------+-------------+------+

到目前为止的解决方案

SELECT *
FROM customer c
# Join the most recent address
LEFT JOIN customer_address ca ON ca.id = (SELECT MAX(ca.id) FROM customer_address ca WHERE ca.customer_id = c.id)
# Join the most recent mobile phone number
LEFT JOIN customer_phone cphm ON cphm.id = (SELECT MAX(cphm.id) FROM customer_phone cphm WHERE cphm.customer_id = c.id AND cphm.`type` = 1)
# Join the most recent landline phone number
LEFT JOIN customer_phone cphl ON cphl.id = (SELECT MAX(cphl.id) FROM customer_phone cphl WHERE cphl.customer_id = c.id AND cphl.`type` = 2)
# Yay conditions appended at the end
WHERE c.id IN (1,2,3)

小提琴

此小提琴使用给定的解决方案给出适当的结果集。请参阅我上面的问题。

http://sqlfiddle.com/#!9/98c57/3

我会

避免那些依赖的子查询,而是尝试这个:

SELECT
      *
FROM customer c
      LEFT JOIN (
            SELECT
                  customer_id
                , MAX(id) AS currid
            FROM customer_phone
            WHERE type = 1
            GROUP BY
                  customer_id
      ) gm ON c.id = gm.customer_id
      LEFT JOIN customer_phone mobis ON gm.currid = mobis.id
      LEFT JOIN (
            SELECT
                  customer_id
                , MAX(id) AS currid
            FROM customer_phone
            WHERE type = 2
            GROUP BY
                  customer_id
      ) gl ON c.id = gl.customer_id
      LEFT JOIN customer_phone lands ON gl.currid = lands.id
WHERE c.id IN (1, 2, 3)
;

或者,也许:

SELECT
      *
FROM customer c
      LEFT JOIN (
            SELECT
                  customer_id
                , MAX(case when type = 1 then id end) AS mobid
                , MAX(case when type = 2 then id end) AS lndid
            FROM customer_phone
            GROUP BY
                  customer_id
      ) gp ON c.id = gp.customer_id
      LEFT JOIN customer_phone mobis ON gp.mobid = mobis.id
      LEFT JOIN customer_phone lands ON gp.lndid = lands.id
WHERE c.id IN (1, 2, 3)
;

参见:http://sqlfiddle.com/#!9/ef983/1/

相关内容

  • 没有找到相关文章

最新更新