0

我对 MySQL 比较陌生,需要一些指导。我有一个名为 events 的表,看起来像这样

---------------------------------------------------------------
| event_id | promoter_id | venue_id | event_date | attendance |
---------------------------------------------------------------
|    0     |      5      |     1    | 2013-05-30 |     400    |
|    1     |      2      |     6    | 2013-06-09 |     104    |
|    2     |      9      |     1    | 2013-05-30 |     300    |
|    3     |      1      |     3    | 2013-06-09 |     205    |
|    4     |      3      |     2    | 2013-06-01 |     175    |
|    5     |      5      |     2    | 2013-06-01 |     295    |
---------------------------------------------------------------

我正在寻找与其他活动在同一地点和日期发生的发起人活动的出席情况。在这个例子中,我想从查询中得到所有活动的出席情况,发起人 5 共享相同的地点和日期。

--------------
| attendance |
--------------
|     400    |
|     295    |
--------------

这个查询对我来说合乎逻辑,但显然我遗漏了一些东西,因为它会产生语法错误......

SELECT t1.attendance FROM events t1 WHERE promoter_id=5 
    JOIN (SELECT event_id, venue_id, event_date, attendance FROM events 
        WHERE promoter_id!=5) t2 
    ON t1.event_id=t2.event_id 
    WHERE t1.venue_id=t2.venue_id 
        AND t1.event_date=t2.event_date;

任何帮助将不胜感激,在我自己的研究中,我还没有得到任何接近我需要的东西。我宁愿在不创建视图的情况下执行此操作,因为我必须在获得结果后删除视图(随着表的增长,我觉得这可能会影响性能,尤其是当我打算为每个发起人运行此查询时)。

谢谢

4

4 回答 4

0
SELECT t1.attendance 
FROM   events t1 
WHERE  promoter_id = 5 
       AND EXISTS (SELECT event_id
                   FROM events 
                   WHERE promoter_id!=5
                         AND venue_id = t1.venue_id=venue_id 
                         AND event_date = t1.event_date)
于 2013-06-10T19:41:09.850 回答
0

如果我理解正确,您将包括 400 和 295,因为其他发起人在同一天在同一地点举办了活动。如果是这种情况,试试这个:

SELECT attendance
FROM events
WHERE promoter_id = 5 AND (venue_id, event_date) IN (
  SELECT venue_id, event_date
  FROM events
  WHERE promoter_id <> 5)
于 2013-06-10T19:42:00.897 回答
0
SELECT attendance FROM events NATURAL JOIN (
  SELECT   venue_id, event_date
  FROM     events
  GROUP BY venue_id, event_date
  HAVING   COUNT(*) > 1
) t WHERE promoter_id = 5

sqlfiddle上查看。

于 2013-06-10T19:43:56.060 回答
0

我看到有很多答案,但要解决您的语法错误:您在 JOIN 之前执行 WHERE 子句。where 子句也是不明确的,因为它在表 t1 和 t2 中都指向promoter_id。

尝试这个:

/* Select sum of attendance columns */
SELECT (t1.attendance + t2.attendance) attendance
FROM events t1
/*Specify Join immediately after FROM clause*/
JOIN events t2
/* declare all join specific conditionals before initiating WHERE clause */
ON t1.venue_id = t2.venue_id
AND t1.event_date = t2.event_date
AND t2.promoter_id != 5
WHERE t1.promoter_id = 5;

上面的查询将为您提供:

--------------
| attendance |
--------------
|     700    |
|     470    |
--------------

要将其分解,您可以分解出勤字段的总和。

SELECT t1.attendance, t2.attendance, t1.venue_id
FROM events t1
/*Specify Join immediately after FROM clause*/
JOIN events t2
/* declare all join specific conditionals before initiating WHERE clause */
ON t1.venue_id = t2.venue_id
AND t1.event_date = t2.event_date
AND t2.promoter_id != 5
WHERE t1.promoter_id = 5;

上面的查询将为您提供:

---------------------------------------
| attendance |  attendance | venue_id |
---------------------------------------
|     400    |       300   | 1        |
|     295    |       175   | 2        |
---------------------------------------
于 2013-06-10T20:23:28.007 回答