[英]Count/Check first instances of entries in mysql table
id | user_id | date_tracked
---------------------------------------------
1001 | 1 | 10-10-2013
1002 | 2 | 10-10-2013
1003 | 3 | 10-10-2013
1004 | 1 | 10-11-2013
1005 | 2 | 10-12-2013
I have a table similar to this, which tracks a user and a date. 我有一个类似于此的表,它跟踪用户和日期。 I need to find how many first-time entries for users occurred on a selected date.
我需要找到在选定日期发生了多少次首次用户条目。 This table is going to hold a significant amount of data, which is why I'd like to rely on a query to process this instead of a bunch of PHP loops.
这个表将包含大量数据,这就是为什么我要依赖查询来处理它而不是一堆PHP循环。
ie on 10-11-2013, user 1 visited but not their first time, so return 0 on 10-10-2013, user 1 and 2 visited for their first time, so return 2. 即在2013年11月10日,用户1访问但不是他们的第一次,所以在10-10-2013返回0,用户1和2第一次访问,所以返回2。
Obviously, using a simple query can count the number of entries on a specified date, but what methodology will allow me to only count if the user_id is not present on a row prior. 显然,使用简单查询可以计算指定日期的条目数,但是只允许我在前一行中不存在user_id时使用什么方法。
The table is ordered by date, meaning that a more recent date should never have a smaller id than an older date. 该表按日期排序,这意味着更新的日期永远不应该具有比旧日期更小的ID。
Any ideas?? 有任何想法吗?? Thanks!
谢谢!
Finding all "first time"-date, by user: 按用户查找所有“第一次” - 日期:
SELECT user_id, MIN(date_tracked) AS first_date
FROM table
GROUP BY user_id
Counting "first time", for each date: 为每个日期计算“第一次”:
SELECT t.first_date, COUNT(*) AS nb
FROM (SELECT user_id, MIN(date_tracked) AS first_date
FROM table
GROUP BY user_id) t
GROUP BY t.first_date
In response to Filipe, 回应菲利普,
Maybe this would be more suitable? 也许这会更合适?
SELECT COUNT(*)
FROM table t1
WHERE t1.date ='2013-8-27'
AND NOT EXISTS (
SELECT 1
FROM table t2
WHERE t2.user_id = t1.user_id
AND t2.id < t1.id
);
SELECT user_id,MIN(date_tracked)FROM table_name GROUP BY user_id
For counting as one, if the user visits multiple times in the first day, you can do it in two ways: 如果计算为一,如果用户在第一天多次访问,您可以通过两种方式进行:
SELECT COUNT(distinct user_id)
FROM user t1
WHERE t1.date_tracked = '2013-10-12'
AND NOT EXISTS (
SELECT 1
FROM user t2
WHERE t2.user_id = t1.user_id
AND t2.date_tracked < t1.date_tracked
);
SELECT COUNT(user_id)
FROM user t1
WHERE t1.date_tracked ='2013-10-12'
AND NOT EXISTS (
SELECT 1
FROM user t2
WHERE t2.user_id = t1.user_id
AND t2.id < t1.id
);
I think i prefer the second, as it is much more cleaner by comparing ID's and not having to do the distinct. 我认为我更喜欢第二种,因为通过比较ID并且不必进行区分,它更清晰。
By your tags I assume you want your answer in php as well. 通过你的标签我假设你也想在PHP中得到你的答案。 Just one query: shouldn't date 10-10-2013 return 3?
只有一个问题:不应该在10-10-2013日期返回3? Anyway you can try this:
无论如何你可以试试这个:
<?php
$dbConnect = @mysqli_connect($host, $user, $pass)or die(); $ dbConnect = @mysqli_connect($ host,$ user,$ pass)或die();
@mysqli_select_db($dbConnect, $dbname) or die();
$query = "SELECT id FROM tablename WHERE date_tracked = '$searchDate' AND (SELECT COUNT(id) FROM tablename WHERE date_tracked < '$searchDate')=0";
$queryResult = @mysqli_query($dbConnect, $query) or die();
$rowCount = mysqli_num_rows($queryResult);
echo $rowCount . "<br/>";
mysqli_close($dbConnect);
?> ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.