I have a table with the following format:
offer_id consumer_id date
1 1 1282454200
1 1 1282453200
2 2 1282453240
1 3 1282455200
2 1 1282453210
"date" is in unix format.
I need to count all of the daily entries, so if I have 10 entries from yesterday and 8 entries from today, I should get:
2013-06-23 10
2013-06-24 8
This is part of my work on trying to optimize code, so far I have been doing this via PHP code, but you can imagine what happens with a growing database :). This is my php (codeigniter) attempt that I'm trying (unsuccessfully) to translate into mysql:
foreach ($offers as $item) {
$date = $item->date;
$day_date = date("Y-m-d", $date);
$day_start = strtotime(date("Y-m-d 00:00:00", $date));
$day_end = strtotime(date("Y-m-d 23:59:59", $date));
if (!in_array($day_date, $day_array)) {
$day_array[] = $day_date;
$this->db->where("date >=", $day_start);
$this->db->where("date <=", $day_end);
$this->db->from("offers_consumers_history");
$total_points = $this->db->count_all_results();
$db_date = array($day_date, $total_points);
$data[] = $db_date;
}
}
I basically grabbed all of the entries in a previous query and went through every date, if the date isn't in my final array, I had to it by counting all results from 00:00:00 to 23:59:59.
Looking for help in building equivalent SQL code.
You could use this SQL query:
SELECT DATE(FROM_UNIXTIME(date)), COUNT(*)
FROM offers_consumers_history
GROUP BY DATE(FROM_UNIXTIME(date))
Please see fiddle here.
Try like
SELECT count(*) as cnt , date FROM `my_table` GROUP BY date
Then you can change them as your required format.It is simple and same that to change the dates into FROM_UNIXTIME and then counting
If I have right understood your question, group by is what you need
Related
I have a table in mySQL workbench that has several columns and 11 million rows.
One of the columns is a time stamp in the following format:
2014-01-01 00:12:54
There is another column which I'm doing a group by on.
My query currently looks something like this:
SELECT app_type, count(series_id) as 'Num of Series Downloaded' from access_log
WHERE action = 'download' AND org_id != 1
GROUP BY app_type;
Which produces a result this:
What I'd like to do is get the average number of series downloaded (series_id) per month given that I have a time column. Keeping in mind there are 5 years of data and 11 million rows.
Ideally, the result would be formatted something like this:
What could my query look like to format a result like what I'm aiming to achieve?
I think you just want a basic aggregation query:
SELECT DATE_FORMAT(`timestamp`, '%Y-%m') as yyyymm,
SUM(app_type = 'API') as API,
SUM(app_type = 'Web') as Web,
SUM(app_type = 'Excel') as Excel
FROM access_log
WHERE `action` = 'download' AND org_id <> 1
GROUP BY yyyymm;
I am using this SQL Query:
SELECT * from reminders where status <> 'c' AND user = '".$InternalUserResult["sequence"]."' AND duedate_date = '".date("Y-m-d")."' ";
it selects rows from a table based on the status and if the duedate_date is set today
i have 3 other columns in the table called duedate_time, remind_user_1 and remind_user_2
lets say in this example
remind_user_1 = 4
remind_user_2 = days
how can i add into my query *if today is 4 days before the due date *
and as another example,
remind_user_1 = 1
remind_user_2 = hour
the query i want to say:
if today is 1 hour before the duedate_date + duedatetime
To get 4 days before date use SUBDATE function in mysql. e.g.
SELECT SUBDATE(NOW(),4);
The DATEDIFF function can be used in the where clause. In this case, the additional condition looks like:
AND DATEDIFF(duedate_date,CURDATE()) = 4
I have been searching Google and SO for the last 25 min searching for how to do the following in MySQL.
I currently have the following query (Sent by PHP):
SELECT
COUNT(*),
`$db`.`crop`.`id` AS `crop`,
`$db`.`crop`.`harvest_date` AS `harvest_date`
FROM
`$db`.`crop`
WHERE
`$db`.`crop`.`harvest_date` BETWEEN $startDate AND $endDate
GROUP BY `$db`.`crop`.`harvest_date`
$startDate = 2012-01-01
$endDate = 2013-07-01
I am trying to find all the rows that have a harvest_date between start and end dates, and then count the number of rows that fall on the same date. However, I seem to be getting no results. The query doesn't fail, it just doesn't return anything. Can anyone point me in the right direction/tell me where I got it wrong?
EDIT: Found the problem. As Michael pointed out below, the dates were not getting passed as dates, but as numbers. I solved this by adding ' before and after startDate and endDate in the query.
MySQL expects date literals to be single-quoted strings like '2012-01-01' and '2013-07-01'.
Since you have not quoted your date literals which are PHP variables, PHP is actually interpreting them as arithmetic on integer values before it passes them into the query.
// You see this:
$startDate = 2012-01-01
$endDate = 2013-07-01
// PHP does this:
// 2012 - 1 - 1 = 2010
$startDate = 2010
// 2013 - 7 - 1 = 2005
$endDate = 2005
Your query ultimately uses this:
WHERE
`$db`.`crop`.`harvest_date` BETWEEN 2010 AND 2005
And MySQL will cast both of those integers to a DATE, which will return NULL.
mysql> SELECT CAST(2010 AS DATE);
+--------------------+
| CAST(2010 AS DATE) |
+--------------------+
| NULL |
+--------------------+
So the simple fix is:
$startDate = '2012-01-01';
$endDate = '2013-07-01';
And if you eventually convert this to a parameterized query, the correct quoting of placeholders would be handled for you.
To get the count between the given date range. Modify the query as
SELECT
COUNT(`$db`.`crop`.`id`),
FROM
`$db`.`crop`
WHERE
`$db`.`crop`.`harvest_date` BETWEEN $startDate AND $endDate
Grouping the result with "harvest_date" will determine the count for that particular date and group them.
example :
if the table is like
8-21-2013
8-21-2013
8-20-2013
then grouping will give
2
1
without grouping
3
I know this is an old post , but this worked for me
SELECT count(*),date(LSTUPDTIM),SUM(TOTAL) FROM order_table where LSTUPDTIM between '2018-01-20' and '2019-01-29' group by date(LSTUPDTIM) ;
,
my LSTUPDTIM is a TimeStamp which is why i had to use date()
which gave me an output like this
5 2019-01-25 500
2 2019-01-28 200
5 2019-01-29 500
I am in a problem and i am confused. I have a table for events and in it there is a column that holds the timestamp of the date time when it was created. I want to get all events(rows) that were created in this month, the last month, and 2 more month before the last month. So, basically 4 months. I want to show them separately so, i can think i can query the datebase for all rows in this month in one query and another query for previous month and so on for 4 months. The problem is i don't know how can i do that. There is a month() function in mysql but i am lost, no idea how to use it.
Any help would be very much appreciated. Thanks
Edit: table structure is like this.
Title | Uri | created
Event | Event| 1337782223
name | uri |
.......
EDIT 2
Thanks all for your help..
I tried it myself and with my friend google...:)
I used this
SELECT * FROM `events` WHERE MONTH(FROM_UNIXTIME(created)) = $month;
and this seems to work for me, i will just pass the month whos rows i want from php thats easier for me...lol
Thanks again to all for your answers its appreciated. This place is awesome
Select * From your_table Group By Month(column_with_date);
Of couse you have to have a timestamp column in your table.
If not then it's impossible to restore the informations when the entry was inserted.
$month = '5,4,3,2';
select colum_name from table_name where month(table_field_name) IN ($thismonth) Group By month(table_field_name)
try this one if you are using separate query for each month
$todayDate = date("Y-m-d");
$thismonth = strtotime($todayDate);
$previous_month = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
and you can get past months just do minus NUMBER ex -2,-3 .....for current month
select colum_name from table_name where month(table_field_name) = month ($thismonth) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name);
for previous month
select colum_name from table_name where month(table_field_name) = month ($previous_month ) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name)
I run an online magazine and would like a dead-simple way to track 3 metrics:
How many comments are left each day.
How many links my users submit.
How many new members I get each day.
This information is all my database, I'm just not sure how to get it out.
I have a "comments" table with about 3500 comments. Each comment gets a row in the table. One of the columns in the table is "timestamp."
I'm assuming there's a query that will select this table, sort the rows by timestamp, group them in 24-hour increments and then count the number of rows in each group - telling me how many new comments I received each day.
What would that query look like? I think I can figure out the other ones if I had the first one working.
This fragment will display your results in a themed table:
$sq = 'SELECT COUNT(*) cnt, DATE(FROM_UNIXTIME(timestamp)) day '
. 'FROM {comments} c '
. 'GROUP BY 2 '
. 'ORDER BY 2 DESC';
$q = db_query($sq);
$stats = array();
while ($o = db_fetch_object($q)) {
$stats[$o->day] = array($o->day, $o->cnt);
}
return theme('table', NULL, $stats));
Using DATE(timestamp) doesn't work because comments.timestamp is in UNIX_TIMESTAMP format, whereas DATE() expects an ASCII date.
Use the date_format() function to format the dates;
select count(id) as commentCount, date_format(dateColumn, '%Y-%m-%d') as commentDate
from yourTable
group by commentDate