[英]MySQL Group By not working in subquery
我有這個查詢,效果很好。 我使用MIN(home_price)
顯示作為起始價格,我使用此查詢為api和WHERE子句添加到它,所以如果我按價格搜索MIN(home_price)
更改。
SELECT MIN(home_price) as min_home_price,
id,
name,
community,
maplocation,
locationLabel,
logo
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0 AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name
所以我的解決方案是使用子查詢
SELECT id,
name,
community,
maplocation,
locationLabel,
logo,
(SELECT MIN(home_price) as min_home_price
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0
AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name) as org_min_home_price
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0 AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name
但是當我執行第二個查詢時,我收到此錯誤
子查詢返回多行
當我刪除GROUP BY
, MIN(home_price)
沒有錯誤,每行都是相同的。 有沒有人對如何完成我想要完成的事情有任何建議?
通常情況下,如果您將price
作為過濾器添加,則返回的最低價格將不是表格的實際最小值。
您可以加入由社區分組的每個min(home_price)的結果集
SELECT min_home_price,
id,
name,
community,
maplocation,
locationLabel,
logo
FROM ourCommunity
INNER JOIN (select min(home_price) min_home_price, home_community from readyBuilt group by home_community) b on b.home_community = community
INNER JOIN readyBuilt a ON community = a.home_community
INNER JOIN rb_locations ON a.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt ON community = home_community
WHERE isDeleted = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name;
不要在MySQL中使用嵌套子查詢來處理大型數據集,否則MySQL最終可能會自己創建臨時表,並且查詢性能將受到影響。
我試圖刪除最后一個嵌套查詢,如下所示:
SELECT b.min_home_price,
oc.id, oc.name, oc.community, oc.maplocation, oc.locationLabel, oc.logo
FROM ourCommunity oc
INNER JOIN (select min(home_price) min_home_price, home_community from readyBuilt group by home_community) b on b.home_community = oc.community
INNER JOIN readyBuilt rb ON oc.community = rb.home_community
INNER JOIN rb_locations rbl ON rb.home_location = rbl.locationId
INNER JOIN readyBuilt rb1 ON oc.community = rb1.home_community
WHERE rb1.isDeleted = 0
AND oc.home_status = 1
GROUP BY oc.id, oc.name, oc.community, oc.mapLocation, oc.locationLabel, oc.logo
ORDER BY oc.name;
如果你能請上傳您的3個表定義,並用簡單的英語總體目標,即WHAT
是你想實現而不是開始HOW
你開始解決這個問題,可能是我可以用更簡單的查詢幫助。
在第二個查詢中,當您沒有組時,只會獲得一行,因為您為整個表選擇了min(home_price)。
當您添加“分組依據”時,您將獲得每個ID,每個名稱,每個社區等的min(home_price)。
您需要更改子查詢以獲取外部查詢的當前行的min(home_price)。 可以將子查詢視為基於某些參數(恰好與外部查詢中的列匹配)返回值的函數。
我還懷疑你在子查詢中只需要一個表 - 一個有價格的表。 其他表可能會為您提供位置分組,因此它們屬於外部查詢。 (嘗試讓外部查詢在沒有最低價格和最后價格的情況下工作)。
例如
選擇id,name等...,(從p中選擇min(price)p.id = oc.id)作為我們社區oc inner join等的minPrice ... group by ...
正如其他答案所示,有很多方法可以實現這一點,例如連接而不是子查詢。 除非性能是一個問題,否則每個都有利弊,只需使用最容易理解和維護的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.