Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Memanipulasi data pengguna di MySQL

Skema

CREATE TABLE log
( user_id int, request_timestamp datetime);

INSERT INTO log
VALUES
(1, '2014-10-26 10:51:18'), (1, '2014-10-26 10:52:20'), (1, '2014-10-26 11:15:03'), (1, '2014-10-26 11:39:18'), (1, '2014-10-26 15:01:18'), (1, '2014-10-26 15:01:21'), (1, '2014-10-27 21:22:19'),
(2, '2014-10-15 12:19:01'), (2, '2014-10-15 12:19:12'), (2, '2014-10-15 12:19:45'), (2, '2014-10-15 12:20:03'), (2, '2014-10-17 14:55:13'), (2, '2014-10-17 14:55:19'),(2, '2014-10-17 14:55:22');

Pertama kami akan memberikan nama berikut hanya untuk memvisualisasikannya:

Perhatikan di bawah 1800 berarti 30 menit * 60 detik/menit

Specimen A
-----  
select l.user_id,l.request_timestamp,
@sessionnum := 
if((@curuser = user_id and TIME_TO_SEC(TIMEDIFF(request_timestamp,@theDt))>1800),@sessionnum + 1, 
if(@curuser <> user_id,1,@sessionnum))  as sessionnum,
@curuser := user_id as v_curuser,
@theDt:=request_timestamp as v_theDt
from log l cross join
(select @curuser := '', @sessionnum := 0,@theDt:='') gibberish
order by l.user_id,l.request_timestamp
+---------+---------------------+------------+-----------+---------------------+
| user_id | request_timestamp   | sessionnum | v_curuser | v_theDt             |
+---------+---------------------+------------+-----------+---------------------+
|       1 | 2014-10-26 10:51:18 | 1          |         1 | 2014-10-26 10:51:18 |
|       1 | 2014-10-26 10:52:20 | 1          |         1 | 2014-10-26 10:52:20 |
|       1 | 2014-10-26 11:15:03 | 1          |         1 | 2014-10-26 11:15:03 |
|       1 | 2014-10-26 11:39:18 | 1          |         1 | 2014-10-26 11:39:18 |
|       1 | 2014-10-26 15:01:18 | 2          |         1 | 2014-10-26 15:01:18 |
|       1 | 2014-10-26 15:01:21 | 2          |         1 | 2014-10-26 15:01:21 |
|       1 | 2014-10-27 21:22:19 | 3          |         1 | 2014-10-27 21:22:19 |
|       2 | 2014-10-15 12:19:01 | 1          |         2 | 2014-10-15 12:19:01 |
|       2 | 2014-10-15 12:19:12 | 1          |         2 | 2014-10-15 12:19:12 |
|       2 | 2014-10-15 12:19:45 | 1          |         2 | 2014-10-15 12:19:45 |
|       2 | 2014-10-15 12:20:03 | 1          |         2 | 2014-10-15 12:20:03 |
|       2 | 2014-10-17 14:55:13 | 2          |         2 | 2014-10-17 14:55:13 |
|       2 | 2014-10-17 14:55:19 | 2          |         2 | 2014-10-17 14:55:19 |
|       2 | 2014-10-17 14:55:22 | 2          |         2 | 2014-10-17 14:55:22 |
+---------+---------------------+------------+-----------+---------------------+

Kemudian kita selesai jika Anda mau. Tapi untuk pencetakan cantik , dapat membungkus Specimen A di dalam yang lain:

select user_id,request_timestamp,sessionnum
from
(   select l.user_id,l.request_timestamp,
    @sessionnum := 
    if((@curuser = user_id and TIME_TO_SEC(TIMEDIFF(request_timestamp,@theDt))>1800),@sessionnum + 1, 
    if(@curuser <> user_id,1,@sessionnum))  as sessionnum,
    @curuser := user_id as v_curuser,
    @theDt:=request_timestamp as v_theDt
    from log l cross join
    (select @curuser := '', @sessionnum := 0,@theDt:='') gibberish
    order by l.user_id,l.request_timestamp
) SpecimenA
order by user_id,sessionnum
+---------+---------------------+------------+
| user_id | request_timestamp   | sessionnum |
+---------+---------------------+------------+
|       1 | 2014-10-26 10:51:18 | 1          |
|       1 | 2014-10-26 10:52:20 | 1          |
|       1 | 2014-10-26 11:15:03 | 1          |
|       1 | 2014-10-26 11:39:18 | 1          |
|       1 | 2014-10-26 15:01:18 | 2          |
|       1 | 2014-10-26 15:01:21 | 2          |
|       1 | 2014-10-27 21:22:19 | 3          |
|       2 | 2014-10-15 12:19:01 | 1          |
|       2 | 2014-10-15 12:19:12 | 1          |
|       2 | 2014-10-15 12:19:45 | 1          |
|       2 | 2014-10-15 12:20:03 | 1          |
|       2 | 2014-10-17 14:55:13 | 2          |
|       2 | 2014-10-17 14:55:19 | 2          |
|       2 | 2014-10-17 14:55:22 | 2          |
+---------+---------------------+------------+
14 rows in set (0.02 sec)

Perhatikan definisi sesi OP. Ini adalah salah satu dari tidak aktif , bukan durasi.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Mysql ERROR 1241 (21000):Operan harus berisi 1 kolom

  2. Jumlah kumulatif MySQL dikelompokkan berdasarkan tanggal

  3. Bagaimana cara memaksa nama tabel peka huruf besar-kecil?

  4. Query berparameter SQL dengan LIKE '% ? %' PHP

  5. Gambar Impor Massal Magento dengan MAGMI - gambar tidak termasuk