0

这确实是两个问题,但我会尽量做到这一点。

我向一位朋友展示了我的初始查询,他告诉我可以通过子选择(在此处使用 MySql )进一步提高性能。

不幸的是,我无法让他的查询起作用,因为我有一个Error Code: 1054 Unknown column 'e.id' in 'where clause'

他在下面的查询:如果有人告诉我为什么它无法识别,将不胜感激e.id

SELECT e.id AS event_id, er.name AS event_name, er.revision AS revision, er.start_date AS event_dates
 FROM `events` AS e
 INNER JOIN 
 (
    SELECT event_id, revision, start_date 
    FROM event_revisions
    WHERE YEAR(start_date) = YEAR(CURDATE())
    AND revision = (
       SELECT MAX(er_tmp.revision) 
       FROM event_revisions AS er_tmp
       WHERE er_tmp.approved = 1
       AND e.id = er_tmp.event_id
    )
 ) AS er ON (e.id = er.event_id)
 INNER JOIN member_events AS me ON (me.event_id = e.id)
 WHERE 
  e.enabled = 1 AND 
  e.deleted = 0
 GROUP BY event_id
 ORDER BY event_dates

这是我的原始查询:

SELECT e.id AS event_id, er.name AS event_name, er.revision AS revision, er.start_date AS event_dates
FROM `events` AS e
INNER JOIN event_revisions AS er ON (e.id = er.event_id)
INNER JOIN member_events AS me ON (me.event_id = e.id)
WHERE 
    e.enabled = 1 AND 
    e.deleted = 0 AND 
    er.revision = (
        SELECT MAX(er_tmp.revision) 
        FROM event_revisions AS er_tmp
        WHERE er_tmp.approved = 1
        AND  e.id = er_tmp.event_id
    )
    AND YEAR(er.start_date) = YEAR(CURDATE())
GROUP BY event_id
ORDER BY event_dates

第二个问题是:为什么他的查询比我的要快?

任何输入都将受到高度赞赏。

4

1 回答 1

1

AD 1. 因为子选择:

 SELECT MAX(er_tmp.revision) 
    FROM event_revisions AS er_tmp
    WHERE er_tmp.approved = 1
    AND  e.id = er_tmp.event_id

为您的查询中的每一行“调用”。

(AD 2),正如@Dor 告诉你的那样,你正在使用来自子查询的 alaias

于 2013-08-01T10:53:17.703 回答