Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:12:15+00:00 2026-06-15T03:12:15+00:00

Say, I have table named ‘member’ id name =================== a John b Frank Member

  • 0

Say, I have table named ‘member’

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

Member have ‘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

I want to create view that have information about when first time login and last time login of the members and also the ‘gol’ value only from the last login.

Is it possible?

I’ve tried many ways. One of my attempt:

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

It does’nt get right result. The ‘gol’ value is from the first login.
How to do this?


Something important here: MySQL doesn’t allow subquery in create view

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:12:18+00:00Added an answer on June 15, 2026 at 3:12 am

    You are ALMOST there. You have the login times, but I would take it and get the last login ID too to simplify a re-join to the login activities table vs trying to do a join on user AND date/time field since I would expect a key on the primary key of ID and not necessarily on (member, login_time) — however, for this query using the min/max on date grouped by member, I would DEFINITELY have an index on (member, login_time) for optimization.

    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
    

    Then, since the above works, but fails due to how MySQL implements views, you might need to do it with TWO views, such that

    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
    

    then another

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have a table named tbl1 in mysql :- bookid int name varchar(20)
lets say I have a table named myPlayerInfo with following rows 'player name' 'number
Lets say I have table named Place with columns: placeId int not null auto_increment,
Say I have a table named items : id int(11) ... tag int(11) And
I have many tables, say T1, T2, T3. Every table has a column named
Lets say i have a table with the following data Customer table: Name amount
Let's say I have a table like this: name |order_id ======================= first_record | 0
Lets say I have a table with columns such as: ID Name City State
Say I have a table called Users, which contains your typical information: Id, Name,
Let's say I have SELECT name FROM table which gives me something like foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.