varchar to datetime in mysql compare - mysql

i have a data like 'ddmmyyyy' like this format in my datebase and it datatype is also varchar(8) now i need to compare the date using between query
my table data is
| id | enquiry_date | dept_name |
|----------------------------------------|
| 1 | 02112004 | LAB |
| 2 | 31122005 | RESEARCH |
| 3 | 26052005 | LAB |
| 4 | 16042006 | RESEARCH |
Now i need to take records from 05-02-2005 to 12-11-2006 like that. here its varchar how to compare it please help me thanks in advance.

You can try using the STR_TO_DATE
STR_TO_DATE(enquiry_date,'%d%m%Y')
So you can compare like
where date_format(STR_TO_DATE(enquiry_date,'%d%m%Y'), '%d-%m-%Y') > '05-02-2005'
and date_format(STR_TO_DATE(enquiry_date,'%d%m%Y'), '%d-%m-%Y') < '12-11-2006'

Related

Add a constant date column in mysql

i have a table in mysql.
+------+---------+
|value | unit |
+------+---------+
| 2 | DAY |
| 3 | MONTH |
+------+---------+
this is just a part of my table. it consists of many rows
i want to add a date column to this table with a constant date. say '2009-01-01'
type of the column should be date.
+------------+------+---------+
| date |value | unit |
+------------+------+---------+
| 2009-01-01 | 2 | DAY |
| 2009-01-01 | 3 | MONTH |
+------------+------+---------+
i want to create table_2
create table table_2 as
select value,unit,`dates` DATE NOT NULL DEFAULT '2009-01-01' from table;
but getting syntax error.
any ideas how to do it?
Try like this:
CREATE TABLE table_2(
dates DATE
) SELECT '2009-01-01' AS dates, value,
unit FROM table_1
for more information Visit here: http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html

How to display the current recent rows according to time?

The following database generated by (select * from users).How can i display the recent rows according to time(i.e for time i had used date +%s).But here it displays all timing .I nee dthe rows with the recent update time.
| time | userid | groupid |
+------------+----------+-----------+
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
| 1476868065 | dnehra | HCG |
| 1476868065 | dprabhu | PDSP_DES |
My expected Output:(but it sould be generated by using some linux commands like date +%s).Is there any linux commands which fetches the only the last recent time rows (or) Is it possible to use inside insert mysql query by storing in some variable name.
| time | userid | groupid |
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
Do you want to represent the time in proper date formant? if so;
select from_unixtime(time), * from user order by time desc
You could specify that you only want items where the time is equal to that of the largest value that column holds in the table.
SELECT * FROM users where time = (Select max(time) from users);

How can I select all rows which have been inserted in the last day?

I have a table like this:
// reset_password_emails
+----+----------+--------------------+-------------+
| id | id_user | token | unix_time |
+----+----------+--------------------+-------------+
| 1 | 2353 | 0c274nhdc62b9dc... | 1339412843 |
| 2 | 2353 | 0934jkf34098joi... | 1339412864 |
| 3 | 5462 | 3408ujf34o9gfvr... | 1339412894 |
| 4 | 3422 | 2309jrgv0435gff... | 1339412899 |
| 5 | 3422 | 34oihfc3lpot4gv... | 1339412906 |
| 6 | 2353 | 3498hfjp34gv4r3... | 1339412906 |
| 16 | 2353 | asdf3rf3409kv39... | 1466272801 |
| 7 | 7785 | 123dcoj34f43kie... | 1339412951 |
| 9 | 5462 | 3fcewloui493e4r... | 1339413621 |
| 13 | 8007 | 56gvb45cf3454g3... | 1339424860 |
| 14 | 7785 | vg4er5y2f4f45v4... | 1339424822 |
+----+----------+--------------------+-------------+
Each row is an email. Now I'm trying to implement a limitation for sending-reset-password email. I mean an user can achieve 3 emails per day (not more).
So I need an query to check user's history for the number of emails:
SELECT count(1) FROM reset_password_emails WHERE token = :token AND {from not until last day}
How can I implement this:
. . . {from now until last day}
Actually I can do that like: NOW() <= (unix_time + 86400) .. But I guess there is a better approach by using interval. Can anybody tell me what's that?
Your expression will work, but has 3 problems:
the way you've coded it means the subtraction must be performed for every row (performance hit)
because you're not using the raw column value, you couldn't use an index on the time column (if one existed)
it isn't clear to read
Try this:
unix_time > unix_timestamp(subdate(now(), interval '1' day))
here the threshold datetime is calculated once per query, so all of the problems above have been addressed.
See SQLFiddle demo
You can convert your unix_time using from_unixtime function
select r.*
from reset_password_emails r
where now() <= from_unixtime(r.unix_time) - interval '1' day
Just add the extra filters you want.
See it here: http://sqlfiddle.com/#!9/4a7a9/3
It evaluates to no rows because your given data for unix_time field is all from 2011
Edited with a sqlfiddle that show the conversion:
http://sqlfiddle.com/#!9/4a7a9/4

MySQL - Select everything from one table, but only first matching value in second table

I'm feeling a little rusty with creating queries in MySQL. I thought I could solve this, but I'm having no luck and searching around doesn't result in anything similar...
Basically, I have two tables. I want to select everything from one table and the matching row from the second table. However, I only want to have the first result from the second table. I hope that makes sense.
The rows in the daily_entries table are unique. There will be one row for each day, but maybe not everyday. The second table notes contains many rows, each of which are associated with ONE row from daily_entries.
Below are examples of my tables;
Table One
mysql> desc daily_entries;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| eid | int(11) | NO | PRI | NULL | auto_increment |
| date | date | NO | | NULL | |
| location | varchar(100) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Table Two
mysql> desc notes;
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| task_id | int(11) | NO | PRI | NULL | auto_increment |
| eid | int(11) | NO | MUL | NULL | |
| notes | text | YES | | NULL | |
+---------+---------+------+-----+---------+----------------+
What I need to do, is select all entries from notes, with only one result from daily_entries.
Below is an example of how I want it to look:
+----------------------------------------------+---------+------------+----------+-----+
| notes | task_id | date | location | eid |
+----------------------------------------------+---------+------------+----------+-----+
| Another note | 3 | 2014-01-02 | Home | 2 |
| Enter a note. | 1 | 2014-01-01 | Away | 1 |
| This is a test note. To see what happens. | 2 | | Away | 1 |
| Testing another note | 4 | | Away | 1 |
+----------------------------------------------+---------+------------+----------+-----+
4 rows in set (0.00 sec)
Below is the query that I currently have:
SELECT notes.notes, notes.task_id, daily_entries.date, daily_entries.location, daily_entries.eid
FROM daily_entries
LEFT JOIN notes ON daily_entries.eid=notes.eid
ORDER BY daily_entries.date DESC
Below is an example of how it looks with my query:
+----------------------------------------------+---------+------------+----------+-----+
| notes | task_id | date | location | eid |
+----------------------------------------------+---------+------------+----------+-----+
| Another note | 3 | 2014-01-02 | Home | 2 |
| Enter a note. | 1 | 2014-01-01 | Away | 1 |
| This is a test note. To see what happens. | 2 | 2014-01-01 | Away | 1 |
| Testing another note | 4 | 2014-01-01 | Away | 1 |
+----------------------------------------------+---------+------------+----------+-----+
4 rows in set (0.00 sec)
At first I thought I could simply GROUP BY daily_entries.date, however that returned only the first row of each matching set. Can this even be done? I would greatly appreciate any help someone can offer. Using Limit at the end of my query obviously limited it to the value that I specified, but applied it to everything which was to be expected.
Basically, there's nothing wrong with your query. I believe it is exactly what you need because it is returning the data you want. You can not look at as if it is duplicating your daily_entries you should be looking at it as if it is return all notes with its associated daily_entry.
Of course, you can achieve what you described in your question (there's an answer already that solve this issue) but think twice before you do it because such nested queries will only add a lot of noticeable performance overhead to your database server.
I'd recommend to keep your query as simple as possible with one single LEFT JOIN (which is all you need) and then let consuming applications manipulate the data and present it the way they need to.
Use mysql's non-standard group by functionality:
SELECT n.notes, n.task_id, de.date, de.location, de.eid
FROM notes n
LEFT JOIN (select * from
(select * from daily_entries ORDER BY date DESC) x
group by eid) de ON de.eid = n.eid
You need to do these queries with explicit filtering for the last row. This example uses a join to do this:
SELECT n.notes, n.task_id, de.date, de.location, de.eid
FROM daily_entries de LEFT JOIN
notes n
ON de.eid = n.eid LEFT JOIN
(select n.eid, min(task_id) as min_task_id
from notes n
group by n.eid
) nmin
on n.task_id = nmin.min_task_id
ORDER BY de.date DESC;

want to get day name for the corresponding date

I have a table setup as shown below.
Table Name: activity.
| ACTIVITY_ID | DATE | ASSIGN_ENGR | TASK_TYPE | TASK_STATUS |
|-------------|------------|-------------|-----------|-------------|
| 1 | 2013-12-31 | Sachin | Monthly | Scheduled |
| 2 | 2013-12-23 | Mikel | Weekly | Done |
| 3 | 2013-10-18 | John | Monthly | Done |
I want to get day name against my date field using query.
MySql Query
SELECT DAYNAME('2007-02-03');
Output:
Saturday
Your Query would be like this
select Activity_ID, Date , DayName(Date) As Day, Assign_Engr, Task_Type,Task_Status From Your_Table_Name;
Dayname() function
Mysql provides with DAYNAME() function.
DATE_FORMAT(NOW(),'%a') # %a: Abbreviated weekday name
For more information: w3schools
A simple query could look like this:
SELECT DATE_FORMAT(date, ‘%a’) AS dateday FROM users WHERE id = 1;