SQL語句構造:選擇唯一記錄

[英]SQL Statement Construction: Selecting Unique Records


I currently have a table of Users, and at what time they connected to a device (e.g. a Wifi Router).

我目前有一個用戶表,以及他們何時連接到設備(例如Wifi路由器)。

+-------------+-----------+---------+------------+---------------------+
| location_id | device_id | user_id | dwell_time | date                |
+-------------+-----------+---------+------------+---------------------+
|          14 |         1 |       1 |  27.000000 | 2014-01-04 00:51:12 |
|          15 |         2 |       1 |  12.000000 | 2014-01-04 01:08:56 |
|          16 |         1 |       1 |  12.000000 | 2014-01-04 01:09:26 |
|          17 |         2 |       1 | 318.000000 | 2014-01-04 01:09:38 |
|          18 |         1 |       2 |  20.000000 | 2014-01-04 01:30:03 |
|          19 |         2 |       3 |  20.000000 | 2014-01-04 01:30:03 |
+-------------+-----------+---------+------------+---------------------+

I need to write a query title "Get Latest User Connections".

我需要編寫一個查詢標題“獲取最新用戶連接”。

Basically, it needs to go through the history table shown above, and pick the latest record (based on Date) for each user and display it. In the example above, the result should be:

基本上,它需要通過上面顯示的歷史表,並為每個用戶選擇最新記錄(基於日期)並顯示它。在上面的示例中,結果應為:

+-------------+-----------+---------+------------+---------------------+
| location_id | device_id | user_id | dwell_time | date                |
+-------------+-----------+---------+------------+---------------------+
|          17 |         2 |       1 | 318.000000 | 2014-01-04 01:09:38 |
|          18 |         1 |       2 |  20.000000 | 2014-01-04 01:30:03 |
|          19 |         2 |       3 |  20.000000 | 2014-01-04 01:30:03 |
+-------------+-----------+---------+------------+---------------------+

Can someone please help me write a SQL statement that does this?

有人可以幫我寫一個這樣做的SQL語句嗎?

3 个解决方案

#1


1  

select *
 from users
 inner join (select user_id,max(date) as maxdate
             from users
             group by user_id)T1
 on T1.user_id = users.user_id
 AND T1.maxdate = users.date

or if you don't want to have a subquery, you can user @variables like this query below

或者如果您不想擁有子查詢,可以像下面的查詢一樣使用@variables

SELECT location_id,device_id,user_id,dwell_time,date,
       IF(@prevUserId IS NULL OR @prevUserId != user_id,@row:=1,@row:=@row+1) as row,
       @prevUserId := user_id
FROM users
HAVING row = 1
ORDER BY user_id,date DESC

here's the sqlFiddle

這是sqlFiddle

#2


2  

Assuming the combination of user_id and date is unique in the table, you could

假設user_id和date的組合在表中是唯一的,您可以

SELECT
  tablename.*
FROM tablename
  INNER JOIN (
    SELECT user_id, MAX(`date`) AS maxdate
    FROM tablename
    GROUP BY user_id
  ) AS selector
  ON tablename.user_id=selector.user_id AND tablename.`date`=selector.maxdate

#3


0  

Try this:

SELECT u.location_id, u.device_id, u.user_id, u.dwell_time, u.datec
FROM (SELECT u.location_id, u.device_id, u.user_id, u.dwell_time, u.date 
      FROM users u ORDER BY u.user_id, u.date DESC
     ) A 
GROUP BY u.user_id

OR

SELECT u.location_id, u.device_id, u.user_id, u.dwell_time, u.date
FROM users u
INNER JOIN (SELECT u.user_id, MAX(u.date) AS latestDate 
            FROM users u GROUP BY u.user_id
           ) A ON u.user_id = A.user_id AND A.latestDate = u.date

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/01/04/72fc44b6b2003603d44a1c052cd679eb.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com