1

说,我有一个名为“成员”的表

    id     name
 ===================
    a      John
    b      Frank

会员有'login_activities'

    id     login_time     gol     member
==========================================
    1      2012-10-01      99      a
    2      2012-10-01     125      b
    3      2012-11-01     255      a
    4      2012-11-02     111      b
    5      2012-11-07     101      a

我想创建一个视图,其中包含有关成员第一次登录和最后一次登录的信息,以及仅来自上次登录的“gol”值。

是否可以?

我尝试了很多方法。我的尝试之一:

CREATE VIEW   view_firstlast
     AS
          SELECT
               MIN(a.login_time) as first_login,
               MAX(a.login_time) as last_login,
               a.gol,
               b.name
          FROM
               login_activities a
                   JOIN member b ON a.member = b.id
          GROUP BY
               a.member

它没有得到正确的结果。'gol' 值来自第一次登录。这该怎么做?


这里有一些重要的东西:MySQL 不允许在创建视图中进行子查询

4

3 回答 3

2

你几乎在那里。您有登录时间,但我也会使用它并获取最后一个登录 ID,以简化重新加入登录活动表与尝试在用户和日期/时间字段上进行连接,因为我希望在ID 的主键,不一定在 (member, login_time) 上——但是,对于使用按成员分组的日期的最小/最大值的查询,我肯定会在 (member, login_time) 上有一个索引以进行优化。

SELECT
      m.Name,
      PreQuery.First_Login,
      PreQuery.Last_Login,
      LA2.GOL
   from
      ( select
              LA.member,
              MIN(LA.login_time) as first_login,
              MAX(LA.login_time) as last_login,
              MAX(LA.ID) as LastLoginID
          FROM
              login_activities LA
          group by
              LA.member ) PreQuery
        JOIN member M
           ON PreQuery.member = M.id
        JOIN login_activities LA2
           ON PreQuery.LastLoginID = LA2.id

然后,由于上述工作,但由于 MySQL 如何实现视图而失败,您可能需要使用两个视图来完成,这样

create view MemberFirstLastOnly
as 
select
      LA.member,
      MIN(LA.login_time) as first_login,
      MAX(LA.login_time) as last_login,
      MAX(LA.ID) as LastLoginID
   FROM
      login_activities LA
   group by
      LA.member

然后另一个

create view MemberLastFirstFinal
as
    SELECT
          m.Name,
          MFLO.First_Login,
          MFLO.Last_Login,
          LA2.GOL
       from
          MemberFirstLastOnly MFLO
            JOIN member M
               ON PreQuery.member = M.id
            JOIN login_activities LA2
               ON PreQuery.LastLoginID = LA2.id
于 2012-11-28T12:08:52.950 回答
0

试试这个:

CREATE VIEW   view_firstlast
     AS
          SELECT
               MIN(a.login_time) AS first_login,
               MAX(a.login_time) AS last_login,
               a.gol,
               b.name
          FROM member b 
               LEFT JOIN login_activities a
                   a.member = b.id
          GROUP BY
               b.member
于 2012-11-28T12:00:47.000 回答
0

您需要再次加入 login_activities 表,但使用 mysql 的一个特殊功能,该功能允许您获取整行 fir 另一列的 max/min:

CREATE VIEW view_firstlast AS
SELECT
    MIN(a.login_time) as first_login,
    MAX(a.login_time) as last_login,
    c.gol,
    b.name
FROM login_activities a
JOIN member b ON a.member = b.id
JOIN (
    SELECT * FROM (
        SELECT gol, member
        FROM login_activities
        ORDER BY login_time desc) x
    GROUP BY member) c ON c.member = b.member
GROUP BY a.member
于 2012-11-28T12:56:40.670 回答