I have a table with a kind of weird date format in a column as varchar - this is the format that the company has provided me with - the T in the middle seems to mess things up.
EVENTID | EVENT_DATE | EVENT_DURATION
1 | 2012-10-14T06:00 | 15
2 | 2012-10-14T06:15 | 11
3 | 2012-10-14T06:26 | 14
4 | 2012-10-14T06:40 | 10
ect...ect
I have php code to return the current time in the exact same format (with the weird 'T' in the middle'
$thisin = DateTime::createFromFormat('Y-m-d\TH:i', date('Y-m-d\TH:i'));
$thisin->setTimeZone(new DateTimeZone('UTC'));
$thisout= $thisin->format('Y-m-d\TH:i');
Assuming that today is the 14th and the current time is 06:21, how do i query the current row based on duration that matches "$thisout from php" and the next five rows (in the future).
Because the current time and date are returned from php as "2012-10-14T06:21" The query should output
2 | 2012-10-14T06:15 | 11 (Now SHowing)
3 | 2012-10-14T06:26 | 14
4 | 2012-10-14T06:40 | 10
ect ect
I have been scratching my head for hours, DATE_FORMAT() Doesn't seem to work, and I think it may be the T in the middle. I am aslo have to figure out how to use duration to determine if the current time applies to a specific row.
This does not work
SELECT DISTINCT EVENTID, EVENT DATE, EVENT_DURATION
FROM epg_event
WHERE DATE_FORMAT(EVENT_DATE, '%Y-%m-%dT%H:%i') >= DATE_FORMAT(NOW(), '%Y-%m-%dT%H:%i')
ORDER BY EVENT_DATE ASC LIMIT 5
Any Ideas?
You should load the date information into a Date column rather than a varchar column in the database. As previously noted this is the ISO format for dates.
Need to add in the interval.
where event_date + INTERVAL duration MINUTE >= NOW()
I'm not currently able to test it, but something like this might work:
SELECT DISTINCT EVENTID, EVENT_DATE, EVENT_DURATION
FROM epg_event
WHERE (event_date <= NOW()) AND (ADDTIME(event_date, INTERVAL event_duration MINUTE) >= NOW())
ORDER BY event_date ASC LIMIT 5
DATE_FORMAT() is meant for formatting a DATETIME field, not the other way around. And, as mentioned earlier, you event_date should be a DATETIME field and you should convert the time when you import the data to your database.
Related
Is there a way to order my SQL results after they have been delivered? So far I have two values. My code currently returns the results and can sort by the dates ORDER BY date ASC
The database entries of interest are.
| date | weeks |
| 2020-07-01 | 2 |
| 2020-07-01 | 3 |
| 2020-07-01 | 5 |
Weeks represents how many weeks away the next date is. so 2020-07-21, 2020-08-03, and so on (I am able to return these values). The order by needs to be able to order the dates and take into account the week value from the closest week to the furthest.
I have tried just updating the database with a new field called final_date, although this makes everything cluttery.
When the data is returned, I can find the new date using:
$addWeeks = strtotime('+'.$numberOfWeeks.' weeks', strtotime($last));
Although, I cannot find a way to order the results by that new value.
The order by needs to be able to order the dates and take into account the week value from the closest week to the furthest.
I think you want:
select t.*
from mytable t
order by t.date + interval t.weeks week
Note that this assumes that column date is of date datatype, not string; else, you need to convert it to a date first, using str_to_date() for example.
I have this certain problem about mysql date functions.
I'm trying to compare the value of THIS MONTH to the given timestamp in database.
For example, month today is june, and the timestamp is 1369967316
And I'm trying to determine if that timestamp is in month of june.
$query = db_query("SELECT * FROM users WHERE MONTH(FROM_UNIXTIME(CURDATE())) = MONTH(1369967316)");
//count total members this mont
$members_month = $query->rowCount();
so if I used the rowCount, the $members_month should have the value of 1.
Unfortunately it doesn't work.
Any help would be appreciated.
Well I saw some answers that some kind of relevant to mine but it doesn't hit the spot or I didn't applied it well.
mysql get month from timestamp not working
how to use curdate() in where clause against unixtimestamp (bigint) column
This works for me:
mysql> SELECT MONTH(FROM_UNIXTIME(1369967316));
+----------------------------------+
| MONTH(FROM_UNIXTIME(1369967316)) |
+----------------------------------+
| 5 |
+----------------------------------+
Your issue is likely coming from the fact that 1369967316 is May 30th, not June (as you expect), thus resulting in an inequality with MONTH(CURDATE()).
mysql> SELECT FROM_UNIXTIME(1369967316);
+---------------------------+
| FROM_UNIXTIME(1369967316) |
+---------------------------+
| 2013-05-30 22:28:36 |
+---------------------------+
for e.g
SELECT *
FROM api_call_log
WHERE account_sid='XXXXXXXXXXXX' AND
created_time >= 01-02-2016 00:00:00 AND
created_time <= 31-02-2016 23:59:59
ALLOW FILTERING
I am getting records from first month as well though I searched for second month.
It's difficult to say without seeing your table structure or relevant portions of your result set. But I did notice that you are not specifying a GMT offset, which means you are effectively querying by your default local offset. The problem, is that Cassandra stores by GMT+0000.
For example, if you have a negative GMT offset of say -0600 (like me), a query for GMT-0600 would miss 6-hours-worth of data from February 1st. For instance, if I have a row out there for 2016-02-01 01:00:00+0000, this query will not return it:
aploetz#cqlsh:stackoverflow> SELECT * FROm events WHERe monthbucket='201602'
AND eventdate >= '2016-02-01 00:00:00';
monthbucket | eventdate | beginend | eventid | eventname
-------------+-----------+----------+---------+-----------
(0 rows)
And that's because 2016-02-01 01:00:00+0000 is essentially 2016-01-31 19:00:00-0600. So if I add the GMT offset of 0000, I see the row.
aploetz#cqlsh:stackoverflow> SELECT * FROm events WHERe monthbucket='201602'
AND eventdate >= '2016-02-01 00:00:00+0000';
monthbucket | eventdate | beginend | eventid | eventname
-------------+--------------------------+----------+--------------------------------------+-------------------
201602 | 2016-02-01 01:00:00+0000 | b | 78d2c2b7-c4ec-408f-be37-eccc0c05727d | test month border
(1 rows)
My guess, is that you probably have the opposite problem (extra rows vs. missing rows) due to having a positive GMT offset. Not specifying your offset in your query could be why it is including rows from the previous month. And if that's the case, then you may want those rows.
Also, don't use ALLOW FILTERING. Like ever.
Try this
SELECT * FROM api_call_log WHERE account_sid='XXXXXXXXXXXX' AND jobs.created_at between '01-02-2016 00:00:00' and '31-02-2016 23:59:59';
And also check the DATE/TIME format of the date column
I mean that if I have table like this:
id | time | name
1 | 1354382314 | test1
2 | 1374769114 | test2
3 | 1322759914 | test3
How to select a records, for example, that was created a week ago, month ago or year ago? Is it possible only with mysql functions or how can I do it in php?
I think it's also possible with mysql functions
Like,
select * from table where time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 WEEK))
select * from table where time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR))
select * from table where time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MONTH))
Since timestamp is a number that always grows, you can simply calculate the start and end stamp of your requested range, and use
WHERE `time` >= 'startstamp' AND `time` <= 'endstamp'
To get a stamp 1 week ago, you can use php functoon strtotime("-1 week"). Similar with month etc.
If you need current stamp for anything, use time().
select id,time,name from your_table where (current_timestamp-time)>7*24*60*60;
7*24*60*60 stands for a week
I'm writing a PHP competition script for a members site that needs to restrict entries to one per day per member. So far I have the following MySQL code:
SELECT ce_id
FROM competition_entries
WHERE ce_c_id = '$c_id'
AND ce_sub_id = '$user_id'
AND cte_date >= SYSDATE() - INTERVAL 1 DAY
ce_c_id is the competition ID,
ce_sub_id is the member ID, and
cte_date is a MYSQL datetime stamp for the entry.
It's hard for me to test from where I am now & I need to find a solution, so I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs - and point me in the right direction if it's the latter.
TIA :)
Create a primary key composed of the user_id, competition_id and a date type column.
To check if the user has already placed an entry:
select count(*)
from competition_entries
where ce_c_id = '$c_id'
AND ce_sub_id = '$user_id'
AND cte_date = current_date()
I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs
Looks like it's 24 hours:
mysql> select sysdate(), sysdate() + interval 1 day;
+---------------------+----------------------------+
| sysdate() | sysdate() + interval 1 day |
+---------------------+----------------------------+
| 2011-03-21 15:50:56 | 2011-03-22 15:50:56 |
+---------------------+----------------------------+
1 row in set (0.01 sec)
If you need "tomorrow", as in, tonight at one minute past 23:59, consider chomping down things to plain old DATE's resolution:
mysql> select DATE(sysdate()), DATE(sysdate()) + interval 1 day;
+-----------------+----------------------------------+
| DATE(sysdate()) | DATE(sysdate()) + interval 1 day |
+-----------------+----------------------------------+
| 2011-03-21 | 2011-03-22 |
+-----------------+----------------------------------+
1 row in set (0.00 sec)
By just considering dates instead of times, your cutoff will effectively expire at midnight. Watch out, though -- you'll then be at the mercy of the time on your MySQL server, which might differ from the time on your application server if they are on different machines.
Can't be sure without testing, but I'd hazard a guess that it's once every 24h.
Try the following instead:
SELECT ce_id FROM competition_entries WHERE ce_c_id = '$c_id' AND ce_sub_id = '$user_id' AND DATE(cte_date) == DATE(SYSDATE())
Here you are clearly comparing two dates, one from your field and the other from the current date for equality.