I have a table with columns username and timestamp . When a username is entered the current timestamp is as well.
I would like to fetch the rows of all the new users from a certain time until now, but the start time varies. Lets make it a timestamp represented by x.
I would like to use SQL to select all rows in a table which have a time-stamp in the time range of timestamp x up until now().
select * from tablename where timestamp between cast(x as timestamp) and now();
Try the statement you had placed in the comments but without the quotes. SELECT username FROM accounts WHERE timestamp > x
And for X you would enter time in 24hr format ie. 15:26
Related
I have rows of user data. I store the createDate, which is the timestamp in milliseconds when the user registered. I want to get the total number of registrations per month. When I try to do that, I don't get any rows returned. Here's the query I'm using
SELECT COUNT(*) FROM users WHERE YEAR(createDate) = 2023 GROUP BY MONTH(createDate)
createDate is BIGINT and is the date in milliseconds
I guess your createDate column is defined as TIMESTAMP(3), to get millisecond resolution. LAST_DAY() comes in handy here.
Try this:
SELECT COUNT(*), LAST_DAY(createDate) month_ending
FROM users
WHERE createDate >= '2023-01-01'
AND createDate < '2024-01-01'
GROUP BY LAST_DAY(createDate)
The date range test I use for getting the dates in a single year is sargable. That is, it can be accelerated by an index on createDate, where YEAR(createDate) cannot be.
This approach generates a useful result set if you run it on a multi-year date range.
But, if your result set is empty (has no rows), the result set from this query will be too. That might mean:
your table has no dates in that range, or
your createDate data type is something other than TIMESTAMP or DATETIME. (You didn't show us the table definition.)
It sounds like you need to convert to/from unix time:
SELECT COUNT(*), LAST_DAY(FROM_UNIXTIME(createDate/1000)) month_ending
FROM users
WHERE createDate >= UNIX_TIMESTAMP('2023-01-01') * 1000
AND createDate < UNIX_TIMESTAMP('2024-01-01') * 1000
GROUP BY month_ending
I hava a table with t.string :created_time column
It contains data in UTC format (e.g. 2017-05-10T06:30:01+0000)
I have to retrieve records from table with certain date but time doesn't matter
date = Date.new(2017,05,10)
nodes = Node.where(created_time: date)
logger.info nodes.count
Here I have 0 count. How to make additional processing of created_time value so I could to compare only date part?
Create a date range and query like this
date = Date.new(2017,05,10)
nodes = Node.where(created_time: date.beginning_of_day..date.end_of_day)
You will get the records for that day ;)
UPDATE
Why on earth is created_at a string??
Let's try this,
Node.where('DATE(nodes.created_time) = ?', date)
Select the date as just date from mysql table like this
SELECT DATE(date_time_field) FROM `yourtable`;
I have a 'timestamp' type column in my table called updated_date. When adding a column to the table, all rows got updated to the same updated_date. Not a disaster as we're still in testing, but it kind of broke the functionality of our site (which shows things in order of updated_date).
Is there a way I can change all the updated_date values in the column (but where id is lower than x) to some random date (or an incremental date)?
Thanks in advance!
This might solve your problem:
UPDATE updated_table SET timestamp = FROM_UNIXTIME(1e9 + id) WHERE id < x;
Basically it sets dates to Unix timestamps corresponding to 1 billion + id (1,000,000,000 unix timestamp is 2001-09-08 21:46:40). That way you get unique timestamps in order of id.
Well, you could do this
UPDATE table SET updated_time = NOW() WHERE id < x
Given id belongs to table
in case you want some random data from the past
UPDATE test2 SET update_time = NOW() - interval rand()*120 day - interval rand()*36000 second WHERE id < x
Tweak it to your needs
Timestamps are just the number of seconds since the epoch (1970-01-01 00:00:01). If you start with a base timestamp, you can just add a random number of seconds since that number and you have random dates.
I am stuck with this issue for a while now any solutions are welcomed.
I have a table in MySQL and i am storing a timestamp.
Now lets say if i search on the basis of only date it works.
SELECT *
FROM table
WHERE timestamp > "2012-03-12";
this fetches all data where timestamp value is greater than 2012-03-12 00:00:00.
what i want is a query like this :
SELECT *
FROM table
WHERE timestamp > "10:20:09";
where all records with tie greater than 10:20:09 is fetched irrespective of the date.
Use this statement (it will get the time from your datetime column and compare it with the time)
SELECT *
FROM table
WHERE DATE_FORMAT(yourDatecolumn, '%H:%i:s') > '10:20:00'
Warning
This time format is for EU times (where 1pm = 13), if you want US format, use '%h:%i:s'
I have a timestamp and I want to search for a single date but I can't figure out how to do this.
SELECT something from mytable WHERE timestamp = 'desiredDate'
If I include the hours mins and seconds in a range I can get it but there has to be a way to tell mysql that you want everything for a single day. Can someone please help? Thanks.
You could use TO_DAYS to turn the timestamp into a day number, then compare that with the desired day number:
SELECT foo FROM mytable WHERE TO_DAYS(timestamp) = TO_DAYS('2009-09-07');
How about
SELECT something from mytable WHERE timestamp > 'desiredDate+00:00:00' and timestamp < 'desiredDate+23:59:59'
Or similar...
You could
SELECT myField FROM myTable WHERE timestamp LIKE '2009-08-07%'
to get all entries of a certain day.