I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.
Related
I have some problem to get previous day of the record from the database. My table look like this :
id total trx_date
1 | 100 | 2019-05-13 04:03:03 |
2 | 200 | 2019-05-13 14:13:48 |
3 | 300 | 2019-05-12 10:23:37 |
4 | 100 | 2019-05-10 12:14:14 | <<<<<<<<<<<
5 | 200 | 2019-05-10 15:23:38 |
6 | 400 | 2019-05-10 19:33:56 |
I'm able to take the previous date if there's no gap between the date, but I'm failed to get the previous date if there's no record on the previous date.
My code :
$this->db->select("id, total, trx_date")
->from("trx_table")
->where('date(trx_date)', date('Y-m-d', strtotime('-1 day')) )
->order_by("id","DESC")
->get();
So I expect to get the previous day of the record, not the previous day of the current date. Can you guys help me to resolve this issue?
Thank you
Select dates earlier than today, but sort them descending and get only one. This will return the latest date earlier than today:
SELECT id, total, trx_date
FROM trx_table
WHERE trx_date < NOW()
ORDER BY trx_date DESC
LIMIT 1
Say we have a posts table that has the columns: id, title, expires_at. We want to show how many posts where not "expired" in each week of every year.
Maybe a simpler way of putting it would be: "a count of posts grouped by weeks of the year where the expires_at date is great then the start of each week"
For example:
-------------------------------------------------
| Year | Week | posts_not_expired |
------------|-----------|-----------------------|
| 2017 | 01 | 22 |
| 2017 | 02 | 103 |
| 2017 | 03 | 7 |
| ... | ... | ... |
| 2009 | 52 | 63 |
|-----------|-----------|-----------------------|
What we have so far:
SELECT
COUNT(id) as posts_not_expired,
YEAR(expires_at) * 100 as Year,
YEARWEEK(expires_at) as Week,
FROM posts
GROUP BY Year, Week
You can use DAYOFWEEK to count non-expired posts for a given week. (where 1 = Sunday,2=Monday,..7=Saturday)
SELECT
YEAR(expires_at) as `Year`,
WEEKOFYEAR(expires_at) as `Week`,
SUM(DAYOFWEEK(expires_at) > 2) as `posts_not_expired`
FROM posts
GROUP BY YEAR(expires_at), WEEKOFYEAR(expires_at)
I want to be able to contrast "last weeks" data against "this weeks" data and get the percent change. (i.e. +1.2% or -.5%)
Here's an example of the MySQL table:
Date | Happy | Sad | Angry | Fearful
2016-04-01 | 2 | 1 | 3 | 0
2016-04-02 | 3 | 1 | 3 | 1
2016-04-03 | 0 | 4 | 1 | 2
2016-04-04 | 1 | 3 | 2 | 1
So pretending that there are at least 14 rows here how would I go about getting the average of the first 7 days, the average of the previous 7 days, and then creating the comparison that shows the percentage difference?
I can get the most recent 7 days averages with the code below, but when I try and repeat it and change the offset it fails:
SELECT AVG(happy), AVG(sad), AVG(angry), AVG(fearful)
FROM table_name
LIMIT 0, 7
Use subqueries to calculate the averages for each week, using date ranges, and join them.
SELECT curWeek.happy AS curHappy, curWeek.sad AS curSad, curWeek.angry AS curAngry, curWeek.fearful AS curFearful,
prevWeek.happy AS prevHappy, prevWeek.sad AS prevSad, prevWeek.angry AS prevAngry, prevWeek.fearful AS prevFearful,
100*(curWeek.happy - prevWeek.happy)/prevWeek.happy AS happyChange,
100*(curWeek.sad - prevWeek.sad)/prevWeek.sad AS sadChange,
100*(curWeek.angry - prevWeek.angry)/prevWeek.angry AS angryChange,
100*(curWeek.fearful - prevWeek.fearful)/prevWeek.fearful AS fearfulChange
FROM (SELECT AVG(happy) AS happy, AVG(sad) AS sd, AVG(angry) AS angry, AVG(fearful) AS fearful
FROM tablename
WHERE date > NOW() - INTERVAL 1 WEEK) AS curWeek
JOIN (SELECT AVG(happy) AS happy, AVG(sad) AS sd, AVG(angry) AS angry, AVG(fearful) AS fearful
FROM tablename
WHERE date BETWEEN NOW() - INTERVAL 2 WEEK AND NOW() - INTERVAL 1 WEEK) AS prevWeek
I can code in PHP but I'm not good with SQL at all. I need to run an update on a table in order to pass in a given user_id and set the "access_end" date for all products the user owns to one year from today's date.
Any help much appreciated
Database is MySQL
Table name is dap_users_products_jn
Relevant Fields in database are:
user_id | access_end_date | product_id
1 | 2012-10-26 | 34
1 | 2012-11-21 | 30
1 | 2012-12-22 | 3
2 | 2012-10-20 | 34
2 | 2012-07-18 | 30
2 | 2012-08-15 | 3
...etc
update dap_users_products_jn
set access_end_date = date_add(now(), interval 1 year)
where user_id = 1
i've a mysql table like this:
| ID | ID_period | date_start | date_end |
| 1 | 1 | 0000-07-01 | 0000-08-31 |
| 2 | 2 | 0000-09-01 | 0000-10-30 |
| 3 | 3 | 0000-11-01 | 0000-12-28 |
| 4 | 4 | 0000-11-01 | 0000-03-31 |
how can i select IDs that are included between this period 0000/07/14 - 0000/08/25 ?
date_start and date_end columns are DATE format.
THE PROBLEM is that if i search for a period (included and intersect) that is ie: from 0000-12-12 to 0000-01-25 i get 0 records from the select, i guess for the year that is '0000'.. how can i fix it ?
another problem is that if i search a period like 11-01 to 12-31 i got 0 results.. because last day od december in date_end is 28.. but if i search for a period 11-01 to 12-31 is because i want all the records included.. so i'd like to get the record having id=3 and id=4
at the moment im using the following query:
SELECT ... WHERE '12' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '15' BETWEEN DAY(date_start) and DAY(date_end)
AND '03' BETWEEN MONTH(date_start) and MONTH(date_end)
AND '28' BETWEEN DAY(date_start) and DAY(date_end)
SELECT *
FROM mytable
WHERE date_start BETWEEN '0000-07-14' AND '0000-08-25'
OR date_end BETWEEN '0000-07-14' AND '0000-08-25'
OR (date_start<'0000-07-14' AND date_end>'0000-08-25')