3

我有一个eventsidvenue列的表。

events.venue列与列匹配venues.id,这是一个相当典型且简单的设置。

一些场所被“损坏”,因为它们缺少关键信息,例如geo_lat. 我正在尝试创建损坏场地的优先级列表,其中venue托管最多events的位于顶部,以便我们知道哪些venues是最重要的修复。

到目前为止,我的查询看起来像;

select x.venue_id as id, count(x.venue_id) as events 
from (
    select * 
    from events e, venues v 
    where e.venue=v.id and v.geo_lat is null 
    group by e.venue
) as x order by events desc

哪个给出错误: 错误:列“e.id”必须出现在 GROUP BY 子句中或用于聚合函数中

我试图实现的输出看起来像;

venue.id | events
---------|-------
      12 |    219
     214 |    141
      36 |     41
    1834 |     22
     931 |      4

仅包含破损的场地。这样,我知道固定场地12是重中之重,依此类推。

4

1 回答 1

5
SELECT
  venue.id,
  COUNT(events.venue)    AS event_count
FROM
  venues
LEFT JOIN
  events
    ON events.venue = venue.id
WHERE
  venue.geo_lat IS NULL
GROUP BY
  venue.id
ORDER BY
  COUNT(events.venue) DESC

允许没有活动的LEFT JOIN场地。 INNER JOIN会将它们过滤掉。)

于 2012-10-16T12:02:54.900 回答