I am trying to achieve the Sum of Hours spent per day, where my mysql database has multiple logging records of every activity they perform (e.g; login, view, update, logout). What i am trying to do is SELECT (all records of a particular day) and find the difference in epoch time between the first and last entry giving me the 'Time Spent' for that period. Later add it by Month etc.
Database schema;
id, userid, time, action
**I can select the first and last entry from the query below:
**
SELECT
(SELECT time FROM log WHERE time(myDate) = DATE(NOW()) ORDER BY time LIMIT 1) as 'first',
(SELECT column FROM table WHERE time(myDate) = DATE(NOW()) ORDER BY time DESC LIMIT 1) as 'last'
But i am guessing a cross join or SUM for all these have to happen. Some guidance would be much appreciated.
Related
Here is my query:
SELECT temp_table.*
FROM
( SELECT COUNT(*) as hits_count
, date
FROM visits
GROUP
BY date
) as temp_table
ORDER
BY temp_table.date ASC
LIMIT 15
I insert a new record into this table each time an user access a page. I need to get those records stacked by their date. It worked untill it hit the limit of 15 days, so now it doesn't show other days, it stops on his limit(15).
To make it clearer, let say I have stored 20 days, it shows just the 1-15 day interval, but i need it to get from db the interval 5-20, and so on.
I think this is what you are looking for:
SELECT temp_table.* FROM (
SELECT COUNT(*) as hits_count, date FROM visits GROUP BY date
) as temp_table ORDER BY temp_table.date DESC LIMIT 15
Not sure about the limit part though.
I have a query that counts the "Xp" difference per day from my database, this all works as it should however it groups from midnight-midnight, what I would like to do is group 3am to 3am.
However another issue I think I may have is that my query may not always have the rows being the exact second at 3am due to the fact that it has to run a huge query and retrieve data from another website per user profile, so it should get all data after 3am, but before maybe 4am or something, so it has enough time to get all of the rows.
my current mysql is:
SELECT FROM_UNIXTIME(date, '%Y%m%d') AS YYYYMMDD, MAX(xp)-MIN(xp) AS xp_gain
FROM skills
WHERE userID = '$checkID'
AND skill = '$skill'
AND date >= '$date'
GROUP BY YYYYMMDD
ORDER BY date ASC
The best way to handle this is to add (if you can) another column that is just a DATE (not a DATETIME) and have this field rollover from one day to the next at 3am, (you can to this by subtracting 3 hours from the current time when doing the INSERT).
This gives you a couple of benefits, especially with a large number of rows:
It is much faster to query or group by a DATE than a range of
DATETIME
It will always query the rows at the exact second of 3am,
regardless of how long the query takes.
I have an entry where users can vote once per day. I''m saving this in my database.
Now I need to check, if the user is allowed to vote on this entry again (after one day).
So far I got this:
SELECT count(*)
FROM entries e
WHERE e.voterID =1
AND e.pID =1
AND e.date < NOW( ) - INTERVAL 1
DAY
But this doesn't work to well, when in the DB there are more entries for the voter and pid. A voter can vote multiples times for the same entry.
if there are more entries for the same user and the same projects, count(*) fives me a value more then 1. etc.
How do I check if the user is allowed to vote again properly?
Thanks.
Did the present user vote for the present pID in the most recent 24 hours? If so, the votecount result in this query will be more than zero.
SELECT count(*) AS votecount
FROM entries AS e
WHERE e.voterID = 1
AND e.pID = 1
AND e.date >= NOW() - INTERVAL 1 DAY
Notice the >= comparison for the date. Your sample code says <.
You will need a compound index on (voterID, pID, date) when you need this query to run efficiently on a large table.
I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;
I have 2 tables:
The first contains data for the current day (per hour) - this table has raw data, that needs to be processed.
The second contains data for previous day (per day) - this table already has the values calculated for every day.
I want to combine this to rows in a MySQL query, so that I return the data for previous dates (per day) and for current day (again, per day).
Currently I am using 2 mysql queries:
//table 1, data for current day
$qry1="
select DATE_FORMAT(completedate,'%Y-%m-%d') as date, SUM(IF(complete = 1,affpayout,0)) as pay, COUNT(*) as leads
from leads
where affiliateid={$_SESSION["id"]} AND completedate>'$date'
GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d')";
//table 2, data for previous days, already processsed, we just need to select it
$qry2="
select DATE(date) as date, affrevenue as pay, totalleads as leads
from leadsdays
GROUP BY DATE(date)";
How can I combine the 2 efficiently (speed performance is an issue)? What would be the best way to do this?
Try to UNION. Also, make sure your tables are indexed properly. Looks like you'll want completedate and affiliateid indexed at least. Not sure how much more efficient two queries is versus one union though...
$qry = "(select
DATE_FORMAT(completedate,'%Y-%m-%d') as `date`,
SUM(IF(complete = 1,affpayout,0)) as pay,
COUNT(*) as leads
from leads
where affiliateid={$_SESSION["id"]} AND completedate>'$date'
GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d'))
UNION
(select
DATE(`date`) as `date`,
affrevenue as pay,
totalleads as leads
from leadsdays
GROUP BY DATE(`date`))
ORDER BY DATE(`date`)";