简体   繁体   中英

How to write this Mysql Query Count?

I have this 2 tables.

Loging Log

| id | datetime   | iduser |
|----|------------|--------|
| 1  | 23/09/2016 | 1      |
| 2  | 22/09/2016 | 1      |
| 2  | 22/09/2016 | 2      |

Contacts

| id | name    | created    | iduser |
|----|---------|------------|--------|
| 1  | John    | 24/09/2016 | 1      |
| 2  | Carl    | 24/09/2016 | 1      |
| 3  | Michael | 20/09/2016 | 1      |
| 4  | David   | 23/09/2016 | 2      |

Basicly, i want to get all the contacts from the contacts table since the last login of every user.

For example. Last login from user id = 1 was 23/09/2016, so i want to get the count of contacts after 23/09/2016 and also get the number of days since last login, desired output would be as following:

 Output

 | last login | days since last login | number of contacts | id user |
 |------------|-----------------------|--------------------|---------|
 | 23/09/2016 | 1                     | 2                  | 1       |
 | 22/09/2016 | 2                     | 1                  | 2       |

Thanks in Advance.

EDIT: Both Values are datetime field

Try this

CREATE TABLE login_log (
  id INT NOT NULL AUTO_INCREMENT,
  datetime DATETIME NULL,
  iduser INT NULL,
  PRIMARY KEY (id));

CREATE TABLE contacts (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(45) NULL,
  created DATETIME NULL,
  iduser INT NULL,
  PRIMARY KEY (`id`));

INSERT INTO  login_log (datetime, iduser) VALUES ('2016-9-23', '1');
INSERT INTO  login_log (datetime, iduser) VALUES ('2016-9-22', '1');
INSERT INTO  login_log (datetime, iduser) VALUES ('2016-9-22', '2');

INSERT INTO contacts (name, created, iduser) VALUES ('john', '2016-9-24', '1');
INSERT INTO contacts (name, created, iduser) VALUES ('carl', '2016-9-24', '1');
INSERT INTO contacts (name, created, iduser) VALUES ('michael', '2016-9-20', '1');
INSERT INTO contacts (name, created, iduser) VALUES ('david', '2016-9-23', '2');

SELECT 
    DATE(MAX(datetime)) 'last login',
    DATEDIFF(NOW(), MAX(datetime)) 'days since last login',
    (SELECT 
            COUNT(*)
        FROM
            contacts c
        WHERE
            created > MAX(l.datetime)
                AND c.iduser = l.iduser) 'number of contacts',
    l.iduser 'id user'
FROM
    login_log l
GROUP BY l.iduser

I got the result as

在此处输入图片说明

Break this down into parts. First, get the maximum datetime for each user:

select l.iduser, max(l.datetime) as maxdt
from log l
group by l.iduser;

Then, you can get the additional information in a variety of ways. Here is one method that uses a correlated subquery:

select lu.*,
       (select count(*)
        from contacts c
        where c.user_id = lu.user_id and
              c.created > lu.maxdt
       ) as contacts_since
from (select l.iduser, max(l.datetime) as maxdt,
             datediff(max(l.datetime), curdate()) as days_since
      from log l
      group by l.iduser
     ) lu;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM