简体   繁体   English

MySQL问题! 只需要从一行中选择唯一值

[英]MySQL problem! Needs to select unique value from one row only

I'm creating this app where it's important to register if a person is active or not on the current day. 我正在创建此应用,在那里注册一个人是否活跃于当日非常重要。 My table structure looks like this: 我的表结构如下所示:

| Field     | Type        | Null | Key | Default           | Extra          |
+-----------+-------------+------+-----+-------------------+----------------+
| id        | int(11)     | NO   | PRI | NULL              | auto_increment | 
| kid_id    | int(11)     | NO   |     | NULL              |                | 
| status    | varchar(50) | NO   |     | NULL              |                | 
| timestamp | timestamp   | NO   |     | CURRENT_TIMESTAMP |                | 

What's important for me; 对我来说重要的是 is to select only values with timestamp equal to the current day. 仅选择时间戳等于当前日期的值。 Next I need to check if the latest status is "active" or "inactive" of course based on kid_id. 接下来,我需要根据kid_id检查最新状态是“活动”还是“非活动”。 What I've figured out so far is this. 到目前为止,我已经弄清楚的是这个。

SELECT kid_id , status , timestamp FROM actions WHERE date(timestamp) = CURDATE() ORDER BY timestamp DESC;

Which returns these values: 返回以下值:

+--------+----------+---------------------+
| kid_id | status   | timestamp           |
+--------+----------+---------------------+
|      4 | active   | 2010-08-23 12:10:03 | 
|      3 | inactive | 2010-08-23 10:53:18 | 
|      3 | active   | 2010-08-23 10:53:10 | 
+--------+----------+---------------------+

Only problem now is that i receive both the active and inactive status with "kid_id" = "3". 现在唯一的问题是我同时收到“ kid_id” =“ 3”的活动和非活动状态。 So my question is how do i only select the latest status from each kid_id. 所以我的问题是我如何仅从每个kid_id中选择最新状态。

In advance thank you very much for your help. 在此先非常感谢您的帮助。 It's appreciated. 感激不尽

---------- EDIT ----------编辑

I'll try to make my point a little more clear. 我会尽力阐明我的观点。

This is my table as it looks right now... 这是我现在的桌子...

+--------+----------+---------------------+
| kid_id | status   | timestamp           |
+--------+----------+---------------------+
|      3 | inactive | 2010-08-23 18:32:19 | 
|      4 | active   | 2010-08-23 12:10:03 | 
|      3 | active   | 2010-08-23 10:53:10 | 
+--------+----------+---------------------+
3 rows in set (0.00 sec)

I want to retrieve these values and only these 我只想检索这些值

|      3 | inactive | 2010-08-23 18:32:19 | 
|      4 | active   | 2010-08-23 12:10:03 |

All of the solutions suggested below returns these values: 以下建议的所有解决方案均返回这些值:

|      4 | active   | 2010-08-23 12:10:03 | 
|      3 | active   | 2010-08-23 10:53:10 |

By the way... Thanks for all the responses so soon I'm really grateful for all the help. 顺便说一句...感谢您的所有回复,我非常感谢所有帮助。

first, your WHERE-clause should read 首先,您的WHERE条款应阅读

WHERE ... AND status = 'active'

If you can have different rows for a single user on a given day that are set to active, you can say 如果您可以将给定日期的单个用户的不同行设置为有效,则可以说

SELECT DISTINCT kid, status ...

Note that this returns unique values for everything you select, so you shouldn't select the timestamp because different timestamps at the same day, for the same user, would again show up as several rows (as they're non-distinct) 请注意,这会为您选择的所有内容返回唯一的值,因此您不应选择时间戳记,因为同一用户在同一天的不同时间戳记将再次显示为几行(因为它们没有区别)

this should do 这应该做

SELECT a1.*
FROM actions AS a1 
   INNER JOIN (SELECT kid_id, MAX(`timestamp`) AS `timestamp` 
                 FROM actions 
                 WHERE date(timestamp) = CURDATE() 
                 GROUP BY kid_id) AS a2 
    ON a2.kid_id = a1.kid_id AND a2.timestamp = a1.timestamp

使用group by kid_id ,它仅按时间戳顺序选择最新行

SELECT kid_id , status , timestamp FROM actions WHERE date(timestamp) = CURDATE() GROUP BY kid_id ORDER BY timestamp DESC

Use group by like this: 像这样使用group by

 SELECT kid_id , status , timestamp, count(*) as TotalMatches FROM actions WHERE date(timestamp) = CURDATE() GROUP BY kid_id ORDER BY timestamp DESC

[EDIT] : For selecting only active statuses: [编辑] :仅选择活动状态:

 SELECT kid_id , status , timestamp, count(*) as TotalMatches FROM actions WHERE date(timestamp) = CURDATE() AND status = 'active' GROUP BY kid_id ORDER BY timestamp DESC

I've been out the office today. 我今天去办公室了。 In the mean time I've figured out a way to solve my problem. 同时,我想出了一种解决问题的方法。 I used a subselect which worked. 我使用了一个有效的子选择。

In the end my code looks a little like ovais.tariq 's 最后,我的代码有点像ovais.tariq

Here is my solution: 这是我的解决方案:

SELECT * 
            FROM (
                SELECT * FROM actions 
                AS a
                WHERE date(timestamp) = curdate() 
                AND timestamp = (
                    SELECT max(timestamp) 
                    FROM actions 
                    AS b
                    WHERE a.kid_id = b.kid_id
                    )
                )
            AS c

Thanks everyone for your help :) 谢谢大家的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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