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
Related
I'd need to replace post dates from wordpress post table.
There are >800.000 post entries with the same date because of a migration.
How can I replace the date by "from row x to row"?
For example:
row 1 - 10.000 should have date 2013-01-02 09:20:10
row 10.001 - 20.000 should have date 2013-02-05 12:30:21
and so on...
Or maybe replacing by post id?
I know there is a sql query to do this, but I can not remember which one and how to use it correctly.
try adding a LIMIT to the sql to update rows:
UPDATE {table}
SET {datefield} = "{desired date}"
WHERE {datefield} = "{bad date}"
LIMIT 10000;
this will update 10000 rows at a time with a new date as desired, however it's not particularly picky about which ones get updated in which order, generally it will be in the database's internal order which is (roughly) chronological.
is there any other part of the data you can use to determine which records should be updated with which date?
This is not what you asked for, but might be better. You can create distinct timestamps, as if a post has been created every X seconds:
update posts
set created = timestamp('2013-01-02 12:00:00') + interval id * 140 second
where 1=1
http://sqlfiddle.com/#!9/a6c7e0/2
You can even make them look random:
update posts
set created =
timestamp('2013-01-02 12:00:00')
+ interval id * 140 second
+ interval floor(rand()*140) second
where 1=1
http://sqlfiddle.com/#!9/b394c/1
I want to retrieve the records of employees who were joined in first quarter or in the first month. I have tried this but am not getting the right answer...
SELECT * FROM table
WHERE DOJ(date_created) = DOJ(CURRENT_DATE - INTERVAL 1 MONTH)
Please help me with this!
Answering the question as clarified in a comment...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND QUARTER(table.doj) = 1
If instead you want "first quarter of prior year"...
SELECT * FROM table
WHERE YEAR(table.doj) = YEAR(CURRENT_DATE) - 1 AND QUARTER(table.doj) = 1
In either case, note that there's no code to include the first month, because that's part of the first quarter. However, if you wanted to make that explicit (at a slight performance hit), you could code it as follows...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND (QUARTER(table.doj) = 1
OR MONTH(table.doj) = 1)
If you run into performance problems because you have a lot of records but only an index on table.doj, you could also write the query over an explicit date range...
SELECT * FROM table
WHERE table.doj >= '2015-01-01' AND table.doj <= '2015-03-31'
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
I'm using PHP/MySQL booking system and i'm using a Google Line Chart to try and display the gross sales from the current month, and also the previous 3 months.
Each booking has a date in the standard phpmyadmin "date" format, so yyyy:mm:dd.
So, im looking to get 4 results from 4 queries, each query filtering out a certain month and grabbing the sum of each booking from that month.
My question is, how can i distinguish between the months in the query? How would i structure the query?
Based on the title:
select * from bookings where MONTH(CURDATE())=MONTH(booking_date);
select * from bookings where MONTH(booking_date) > MONTH(CURDATE()-INTERVAL 3 MONTH) and < MONTH(CURDATE() + INTERVAL 1 MONTH);
For simple per-month searches you can use the following:
Select * from bookings where MONTHNAME(booking_date)='July' and YEAR(booking_date)=2013;
Or:
Select * from bookings where MONTH(booking_date)=7 and YEAR(booking_date)=2013;
Also since you've already got the months, you could do this (this method requires that you maintain a table of ending dates for each month an compensate for leap year though):
select * from bookings where booking_date>'2013-06-30' AND booking_date<'2013-08-01';
In first place, excuse my english....
I know this is old thread and cant comment but, #AbsoluteƵERØ, that answer apply to the current month, in example, if i got records of July in 2013-2014-2015, the query will return the records on the month for those years.... To avoid that and using your posted code:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(CURDATE()) = YEAR(booking_date);
Note: if use the "name form" and specify the year there's no problem, like this:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(booking_date) = 2013;
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)