[英]Sql count with inner join and distinct values
我當前正在處理查詢,我想從多個表返回行,並且這些行必須是不同的。 但是,盡管我只想返回1個不同的行,但我也想知道有多少行而不必執行另一個查詢,這就是我到目前為止所得到的:
SELECT DISTINCT a.title,
a.lat,
a.lon,
a.alert_content_id,
a.date_added,
count(*)
FROM `reply` r
INNER JOIN `alerts` a ON r.alert_id=a.alerts
WHERE `user` = '1'
此選項計算要回復的行數,僅返回一行。 但是它必須是具有以下r.alert_id=a.alerts
行: r.alert_id=a.alerts
這將返回所有行,但會丟失每個單獨行的計數
SELECT DISTINCT a.title,
a.lat,
a.lon,
a.alert_content_id,
a.date_added
FROM `reply` r
INNER JOIN `alerts` a ON r.alert_id=a.alerts
WHERE `user` = '1'
有一個答案,但它並沒有完全返回我需要的內容,我需要計算用戶已對警報發表評論的所有答復,而不僅僅是統計用戶對該警報發表評論的次數。
有人可以指出我正確的方向嗎? 還是提供答案? 謝謝。
這是你想要的嗎?
SELECT a.title,
a.lat,
a.lon,
a.alert_content_id,
a.date_added,
count(*)
FROM `reply` r INNER JOIN
`alerts` a
ON r.alert_id = a.alerts
WHERE `user` = '1'
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added;
問題的答案是,您不想使用select distinct
。 您要進行使用group by
的聚合查詢。 只是不清楚要在group by
子句中輸入什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.