MySQL - 无法在左连接和 2 个不同的 select 边界内检索最大值

MySQL - can't retrieve max value within a left join and 2 distinct select perimeters

我正在使用 MySql 5.6 并且我有 2 个表(简化它们以将列的 nb 减少到对这个问题重要的部分),查询是今天使用 LEFT JOIN 到 Select 行。

这里是 fiddle 来测试你的解决方案:https://www.db-fiddle.com/f/nGVFhr3xMwKk9CDw6N6FWc/13

Table 'query_results'

+-----------------------------+------------+--------------+-----------
| query_result_id             | query_id   | author       |  datecol
+-----------------------------+------------+--------------+-----------
| 100                         |         1  | john         |   80
| 101                         |         1  | eric         |   70
| 102                         |         2  | emily        |   100
| 103                         |         2  | emily        |   100
| 104                         |         4  | emily        |   120
| 105                         |         3  | emily        |   50
+-----------------------------+------------+--------------+-----------

Table 'customers_emails'

+-------------------+-----------------+--------------+-----------+-------------+
| customer_email_id | query_result_id | customer_id  | author    |  email_nb 
+-------------------+-----------------+--------------+-----------+-------------+
| 5                 |         758     | 12           |  mathew   |   0  
| 12                |         102     | 12           |  emily    |   0     
| 13                |         103     | 12           |  emily    |   1
| 14                |         104     | 12           |  emily    |   9
| 15                |         102     | 7            |  emily    |   2
+-------------------+-----------------+--------------+-----------+-------------+

我当前的查询成功获取了给定 query_id = 2 和给定 customer_id = 12 的所有 query_results 和一些其他次要约束(例如 datecol > 30)。

  SELECT            
    qr.query_result_id,
    qr.query_id,
    qr.author
  FROM
    query_results qr
  LEFT JOIN
    customers_emails coe
  ON
    qr.author = coe.author AND
    coe.customer_id = 12           
  WHERE        
    qr.query_id = 2 AND
    qr.datecol >= 30 AND
    qr.author IS NOT NULL            
      AND qr.author NOT IN (
        SELECT author
        FROM customers_emails
        WHERE 
          (
            customer_id = 12 AND
            email_nb = 3
          )
      )
  GROUP BY
    qr.author
  ORDER BY 
    qr.query_result_id ASC
  LIMIT 
    2

上面的这个查询非常有效(我很满意)并给我:

今天我的输出是:

+-------------------+-----------------+--------------+
| query_result_id   | query_id        | author       | 
+-------------------+-----------------+--------------+
| 102               |         2       | emily        |                 
+-------------------+-----------------+--------------+

现在我的目标和我失败的地方是:我只是想 添加一个名为 max_email_nb 的新列到当前输出,这将是 **“最高email_nb 已发送

我尝试使用 MAX():

      SELECT            
        qr.query_result_id,
        qr.query_id,
        qr.author,
        MAX(coe.email_nb) as max_email_nb
      FROM
        query_results qr
      LEFT JOIN
        customers_emails coe
      ON
        qr.author = coe.author AND
        coe.customer_id = 12           
      WHERE        
        qr.query_id = 2 AND
        qr.datecol >= 30 AND
        qr.author IS NOT NULL            
          AND qr.author NOT IN (
            SELECT author
            FROM customers_emails
            WHERE 
              (
                customer_id = 12 AND
                email_nb = 3
              )
          )
      GROUP BY
        qr.author
      ORDER BY 
        qr.query_result_id ASC
      LIMIT 
        2

今天我的输出是:

+-------------------+-----------------+--------------+-----------------+-------------
| query_result_id   | query_id        | author       |  max_email_nb   | ... 
+-------------------+-----------------+--------------+-----------------+------------
| 102               |         2       | emily        |  9              |                
+-------------------+-----------------+--------------+-----------------+------------

max_email_nb 中的值不正确 : 根据我的目标,我希望 max_email_nb 的值等于 1 而不是 9。我期望的输出是:

+-------------------+-----------------+--------------+-----------------+-------------
| query_result_id   | query_id        | author       |  max_email_nb   | ... 
+-------------------+-----------------+--------------+-----------------+------------
| 102               |         2       | emily        |  1              |                
+-------------------+-----------------+--------------+-----------------+------------

确实,我想为我的 SQL 查询输出的每个 query_result 检索由 given Customer_id 12query_id 2 上发送的最高 email_nb对此 author(在来自当前 SQL 查询的这一特定行中它是 emily)。

那么这个不正确的 9 值是从哪里来的呢?它来自此输入行:

+-------------------+-----------------+--------------+-----------+-------------+
| customer_email_id | query_result_id | customer_id  | author    |  email_nb   | 
+-------------------+-----------------+--------------+-----------+-------------+
| 14                |         104     | 12           |  emily    |   9  

... 所以它与 query_result_id= 104 相关联,它本身在此处定义:

+-----------------------------+------------+--------------+-----------
| query_result_id             | query_id   | author       |  datecol
+-----------------------------+------------+--------------+-----------
| 104                         |         4  | emily        |   120

...因此 query_id = 4 !但正如我在定义我的目标时所说的那样,我正在寻找与 query_id = 2 相关联的东西,这就是为什么我不应该得到 9 但值 1!

这里有一个 fiddle 来测试你的解决方案:https://www.db-fiddle.com/f/nGVFhr3xMwKk9CDw6N6FWc/13

我试过子查询,我试过内连接...但没有任何效果。

您似乎在 ON 语句中遗漏了一个子句。这行得通吗?

  SELECT            
    qr.query_result_id,
    qr.query_id,
    qr.author,
    MAX(coe.email_nb) as max_email_nb
  FROM
    query_results qr
  LEFT JOIN
    customers_emails coe
  ON
    qr.author = coe.author AND
    qr.query_result_id = coe.query_result_id
  WHERE        
    qr.query_id = 2 AND
    qr.datecol >= 30 AND
    qr.author IS NOT NULL            
      AND qr.author NOT IN (
        SELECT author
        FROM customers_emails
        WHERE 
          (
            customer_id = 12 AND
            email_nb = 3
          )
      )
  GROUP BY
    qr.author
  ORDER BY 
    qr.query_result_id ASC
  LIMIT 
    2

我在 fiddle 上试过了,效果很好,

你也可以试试这个。

SELECT            
    qr.query_result_id,
    qr.query_id,    
    qr.author,
    MAX(coe.email_nb) as max_email_nb
  FROM
    query_results qr
  LEFT JOIN
    customers_emails coe
  ON
    qr.author = coe.author  
    and coe.customer_id = 12
    and qr.query_result_id = coe.query_result_id
  WHERE 
    qr.query_id = 2 AND
    qr.datecol >= 30 AND
    qr.author IS NOT NULL            
      AND qr.query_result_id NOT IN (
        SELECT query_result_id
        FROM customers_emails
        WHERE 
          (
            customer_id = 12 AND
            email_nb = 3
          )
      )
  GROUP BY
    qr.author
  ORDER BY 
    qr.query_result_id ASC
  LIMIT 
    20

这应该可以满足您的需求,无需非确定性 LIMIT 逻辑。

SELECT            
    qr.query_result_id,
    qr.query_id,
    qr.author,
    MAX(coe.email_nb)
  FROM
    query_results qr
  LEFT JOIN
    customers_emails coe
  ON
    qr.author = coe.author AND
    qr.query_result_id = coe.query_result_id AND
    coe.customer_id = 12      
  WHERE        
    qr.query_id = 2 AND
    qr.datecol >= 30 AND
    qr.author IS NOT NULL            
      AND qr.query_result_id NOT IN (
        SELECT query_result_id
        FROM customers_emails
        WHERE 
          (
            customer_id = 12 AND
            email_nb = 3
          )
      )
  ORDER BY 
    qr.query_result_id ASC

你可以看看Fiddle result


附加信息:

你的9而不是1的原因是你的LEFT JOIN条件不充分。

您仅通过在两列中匹配 AUTHOR 加入,但 emily 也有一个 email_nb 等于 9 的条目。

为了获得正确的结果,您还必须根据 QUERY_RESULT_ID 加入,以便将 emily 限制为仅查询 102.[=24= 的结果]