2017-04-18 164 views
0

這是我與工作查詢:MySQL的聚合函數JOIN查詢

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顯示爲每個​​。

這是所期望的結果:

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

我試圖通過使用所述第一內內的聚合MAX(mav.id)功能JOIN,但是沒有成功。

有什麼想法?

+0

燦您在_tabular_表單中顯示示例_table_數據以及預期輸出?使用JSON數據並且沒有表結構有點困難。 –

+0

你能準確地顯示你試過了什麼樣的聚合函數嗎? – roetnig

+0

請閱讀此:http://meta.stackoverflow.com/a/271056/請告訴我們你將如何決定什麼是「最近的」。您沒有任何'DATETIME'或'TIMESTAMP'數據項,所以您將離開我們,也許您自己,猜測您的數據系統的新近程度如何。 –

回答

0

幾件事情:

  1. 在我看來,你需要的所有mav_proposals表,得到每mav_ID最大的查詢。 (SELECT max(mav_proposal_ID) max_Proposal, Mav_ID FROM mav_proposals GROUP BY MAV_ID)

  2. 我不知道什麼是存在你買......所以我消滅它

  3. 我不知道爲什麼一個內嵌​​存在。所以我消除了它。

假設:

  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 

這裏的進近(最慢通常):

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

它在rextester上有語法錯誤檢查它。 – GiamPy

+0

已更新。與正在運行的查詢。 – xQbert

+0

該查詢確實給出了預期的數據集,但它顯着地改變了結果結構。沒有辦法維護它嗎? – GiamPy

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`) 

基本上我只是在你的內連接添加組。我不確定你是否想擺脫重複。

+0

它不能解決問題,您可以看到在多個提案中使用了相同的mav_id。我只想要每個MAV的最新提案。 http://puu.sh/vocDR/6970b1a411.png – GiamPy