Display Time in select query - Mysql - mysql

I got an existing Mysql table with one of the columns as time int(10)
The field has many records like
1455307434
1455307760
Is it a date time, encrypted.
What should be the select Query, so it should display an actual date.

FROM_UNIXTIME()
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable

Related

Best index for mysql date field

I´ve got a table with 41,000,000 rows with the following structure:
What is the best index for date to enable a quick filtering like this
select count(*) from market_db_candle where date >= 2018-09-05 and date <= 2018-09-09;
For the query you show, you can't do better than the index on date which you already have. The query will examine all rows that match the date range, but at least it won't examine any rows outside the date range. If that makes the query examine thousands or millions of rows, that can't be helped. It has to examine the rows to count them.
If you need a query that counts the rows in less time, one strategy is to create a second table stores one row per date, and the count associated with that date.
CREATE TABLE market_db_candle_count_by_day (
date DATE PRIMARY KEY,
count INT UNSIGNED NOT NULL
);
Populate it with the counts by day:
INSERT INTO market_db_candle_count_by_day
SELECT date, COUNT(*)
FROM market_db_candle
GROUP BY date;
Then you can query the SUM of counts:
select sum(count) as count from market_db_candle_count_by_day
where date >= '2018-09-05' and date <= '2018-09-09';
It's up to you to keep the latter table in sync, and update it when necessary.
PS: Put date literals inside single-quotes.

MySQL Querying Unix Timestamps

I have a table that stores the date/time as a Unix timestamp.
Is it possible to query the table and pick out all the entries that are a Monday without having to query all the rows and process them outside of MySQL?
Thanks!
SELECT * FROM your_table WHERE DAYOFWEEK(FROM_UNIXTIME(your_unix_timestamp_column)) = 2

records from table where date between two years

I need to get the record from one table where date between June-30-2011 and June-30-2012.
the problem is that the result is just only display the records of year 2012 although the table has records for year 2011.
below is my code
SELECT * FROM tbl_name where date between '06/30/2011' and '06/30/2012'
you need to convert it bact to date using STR_TO_DATE, eg
SELECT *
FROM tbl_name
where STR_TO_DATE(date, '%m/%d/%Y') between '2011-06-31' and '2012-06-31'
STR_TO_DATE
It is not good to store Dates as string on database because as you see it is hard to search for it, you need some extra functions to convert it back to date and to which I think it kills the index.
If you have time or privilege to alter, fix the values and change it to DateTime data type.

How to select the time between NOW() and last row inserted?

What i basically need is a query that will allow me to display the time that has passed between last row inserted and the current time.
The specific table uses the timestamp field type.
I know that DATE_SUB must be used here, but i have no idea how to in this particular problem
Assuming that table has a timestamp field which records the time-of-insertion for each record, then
SELECT TIMEDIFF(now(), max(timestampfield))
FROM yourtable
select min(now() - timestamp) from table

MySQL query indexing with timestamp structure

I was wondering what the best way of storing user queries correlated with timestamps in MySQL was. Let's say I have just two inputs, a user's "query" and "timestamp"...
I could create a MySQL table with fields (id, query, count, timestamp_list), where:
id is unique identifier of the query,
query is the literal query string,
count is the (constantly-UPDATEd) number of times that query is entered, and
timestamp_list is a LONGTEXT or something with a list of times that query was searched.
Is there a better way to correlate these using indexing I'm not familiar with? It seems like storing a list of timestamps in a LONGTEXT is dumb, but easy; perhaps I should create a separate table like:
id
query_id (correlates to id in first table)
timestamp
And I can join results with the first table. Any thoughts? Thanks!
If you need to record the timestamp when each query was performed, i'd suggest you have 2 tables:
tbl_queries
- id INT
- query VARCHAR
tbl_queries_performed
- id INT AUTOINCREMENT
- query_id INT
- timestamp CURRENT_TIMESTAMP
Each time you want to record a query, check if it's in tbl_queries already and then save an entry in tbl_queries_performed with the query_id respectively