INNER JOIN查询中的MySQL聚合函数

时间:2017-04-18 10:29:52

标签: mysql

这是我正在使用的查询:

SELECT *
FROM `mav_proposals`
INNER JOIN
  (SELECT id AS mav_id, 
             mav_proposal_id
   FROM mav) AS mav ON `mav_proposals`.`mav_id` = `mav`.`mav_id`
WHERE `paid_at` IS NULL
  AND (`mav`.`mav_proposal_id` IS NULL
       OR `mav_proposals`.`sold_at` IS NOT NULL)
  AND `hidden` = 0
  AND EXISTS
    (SELECT *
     FROM `mav`
     WHERE `mav_proposals`.`mav_id` = `mav`.`id`)

这是问题的rextester:http://rextester.com/HDI75486

我希望每个mav_proposal只显示最近的mav

这是理想的结果:

+-----+--------+----------+-------+---------------------+---------+---------+--------+--------+-----------------+
|  id | mav_id | buyer_id | price |          created_at | sold_at | paid_at | hidden | mav_id | mav_proposal_id |
+-----+--------+----------+-------+---------------------+---------+---------+--------+--------+-----------------+
|  1  |      1 |     3704 | 10800 | 15.03.2017 17:56:25 |    NULL |    NULL |  false |      1 |            NULL |
|  4  |      2 |     4462 | 10800 | 09.03.2017 16:27:22 |    NULL |    NULL |  false |      2 |            NULL |
+-----+--------+----------+-------+---------------------+---------+---------+--------+--------+-----------------+

我尝试在第一个INNER JOIN中使用聚合MAX(mav.id)函数,但没有成功。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个,

SELECT *
FROM `mav_proposals`
INNER JOIN
  (SELECT id AS mav_id, 
             mav_proposal_id
   FROM mav group by id) AS mav ON `mav_proposals`.`mav_id` = `mav`.`mav_id`
WHERE `paid_at` IS NULL
  AND (`mav`.`mav_proposal_id` IS NULL
       OR `mav_proposals`.`sold_at` IS NOT NULL)
  AND `hidden` = 0
  AND EXISTS
    (SELECT *
     FROM `mav`
     WHERE `mav_proposals`.`mav_id` = `mav`.`id`)

基本上我只是在你的内连接中添加了group by。我不确定你是否想摆脱重复。

答案 1 :(得分:0)

少数事情:

  1. 在我看来,您需要查询所有mav_proposals表,该表获取每个mav_ID的最大值。 (SELECT max(mav_proposal_ID) max_Proposal, Mav_ID FROM mav_proposals GROUP BY MAV_ID)

  2. 我不知道存在是什么让你买...所以我把它消灭了

  3. 我不知道为什么内联mav存在。所以我把它消灭了。
  4. 假设:

    1. 这确实假设max_proposal的max(ID)始终是mav_ID的“最新”。
    2. 接受这个我们可以将内联视图合并为一个存在或一个连接:这里是连接方法(通常是第二快)

      SELECT m.Id, Buyer_ID, price, Created_at, Sold_at, Paid_at, hidden,  m.ID, mav_proposal_ID
      FROM MAV M
      INNER JOIN mav_proposals MP 
       on M.id = MP.mav_id
      INNER JOIN (SELECT max(ID) max_Proposal, Mav_ID
                  FROM mav_proposals
                  GROUP BY MAV_ID) A
        on MP.ID = A.Max_Proposal
       and M.ID = A.Mav_ID
      WHERE MP.paid_at IS NULL
        AND (M.mav_proposal_id IS NULL  
          OR MP.sold_at IS NOT NULL)
        AND MP.hidden = 0 
      

      这是in方法(通常最慢):

      SELECT m.Id, Buyer_ID, price, Created_at, Sold_at, Paid_at, hidden,  m.ID, mav_proposal_ID
      FROM MAV M
      INNER JOIN mav_proposals MP 
       on M.id = MP.mav_id
      
      WHERE MP.paid_at IS NULL
        AND (M.mav_proposal_id IS NULL  
          OR MP.sold_at IS NOT NULL)
        AND MP.hidden = 0 
      
       and (MP.ID, M.ID)  IN (SELECT max(ID) max_Proposal, Mav_ID
                  FROM mav_proposals
                  GROUP BY MAV_ID)
      

      这里存在(通常最快)

      SELECT m.Id, Buyer_ID, price, Created_at, Sold_at, Paid_at, hidden,  m.ID, mav_proposal_ID
      FROM MAV M
      INNER JOIN mav_proposals MP 
       on M.id = MP.mav_id
      
      WHERE MP.paid_at IS NULL
        AND (M.mav_proposal_id IS NULL  
          OR MP.sold_at IS NOT NULL)
        AND MP.hidden = 0 
       and Exists (SELECT max(ID) max_Proposal, Mav_ID
                   FROM mav_proposals MP1
                   GROUP BY MAV_ID
                   HAVING max_proposal = mp.id
                      and mp1.mav_id = m.id)