I am trying to create a query to only show people who are older than 50 years of age, but i am having difficulty as I only have DOB and not an age field.
Any suggestions? Thanks
Assuming that you save the DOB in date or datetime datatype you can use timestampdiff as
select * from table_name
where timestampdiff(year,dob,curdate()) > 50
Here is a test case
mysql> select timestampdiff(year,'1960-01-20',curdate()) as age ;
+------+
| age |
+------+
| 55 |
+------+
1 row in set (0.00 sec)
mysql> select timestampdiff(year,'1950-01-20',curdate()) as age ;
+------+
| age |
+------+
| 65 |
+------+
1 row in set (0.00 sec)
Assuming dob column Datatype is DateTime
SELECT *
FROM tblname
where YEAR(CURDATE()) - YEAR(dob) > 55
DEMO SQL FIDDLE
Related
I am having problem while getting the result i want from database, DATE_FORMAT function is returning null instead or returning a formatted date.
table columns and their datatype:
paid_amount -> float
created_at -> varchar(100)
bellow is the query is:
SELECT SUM(paid_amount) AS amount,
date_format(created_at,'%Y-%m-%d') as dated
FROM `job_card`
WHERE date_format(created_at,'%Y-%m-%d') >= '2017-09-03' AND
date_format(created_at,'%Y-%m-%d') <= '2017-10-03' GROUP BY date(created_at)
I am using another approach of converting date to time stamp using UNIX_TIMESTAMP function but the still getting the same issue:
SELECT SUM(paid_amount) AS paid_amount,
UNIX_TIMESTAMP(created_at) AS duration
FROM job_card
WHERE UNIX_TIMESTAMP(created_at) >= 1504549800 AND
UNIX_TIMESTAMP(created_at) <= 1507055400 GROUP BY date(payment_date)
You can't use the date functions on a varchar field. You'll need to convert created_at to a date field.
That will make your SQL a lot cleaner as well as you can just use clauses like created_at > '2017-09-03' directly in your query.
I don't know how your dates are formatted currently as strings, but your best bet is probably to write something to read the dates out, format them as YYYY-MM-DD, and re-insert them. If all the dates are that format you should be able to convert that field to date type and keep the information intact.
mysql> desc test;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| field1 | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from test;
+------------+
| field1 |
+------------+
| 2017-01-01 |
+------------+
1 row in set (0.00 sec)
mysql> alter table test modify field1 date;
Query OK, 1 row affected (0.24 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc test;
+--------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------+------+-----+---------+-------+
| field1 | date | YES | | NULL | |
+--------+------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from test;
+------------+
| field1 |
+------------+
| 2017-01-01 |
+------------+
1 row in set (0.00 sec)
I need to get Month number from event table using a mysql select query. here my EVENT table,
query
SELECT month(str_to_date(month,'%d')) AS m FROMevent``
but the result was
how can I convert my month column as numeric..
Use %M insted of %d
mysql> SELECT month(str_to_date('April','%M')) AS m;
+------+
| m |
+------+
| 4 |
+------+
1 row in set (0.05 sec)
In a table X in mysql db i want to insert random anniversary date for every row having date of birth before 1/01/1990 in a new empty column. Please help me out as i m a novice in MySQL
You can use RAND() to generate a random integer in a range (let's say in a range between 23 and 75) like this
SELECT FLOOR(23 + RAND() * (75 - 23))
Your update statement to fill anniversary column of your table then might look like
UPDATE person
SET anniversary = dob + INTERVAL FLOOR(23 + RAND() * (75 - 23)) YEAR
WHERE dob < '1990-01-01'
Let's do a quick test
mysql> CREATE TABLE person
-> (
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> name varchar(32) ,
-> dob DATE ,
-> anniversary DATE
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO person (name, dob)
-> VALUES
-> ('John', '1972-01-15'),
-> ('Phil', '1964-05-23'),
-> ('Mark', '1948-12-10'),
-> ('Steven', '1991-02-28'),
-> ('Helen', '1987-07-01');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> UPDATE person
-> SET anniversary = dob + INTERVAL FLOOR(23 + RAND() * (75 - 23)) YEAR
-> WHERE dob SELECT * FROM person;
+----+--------+------------+-------------+
| id | name | dob | anniversary |
+----+--------+------------+-------------+
| 1 | John | 1972-01-15 | 1999-01-15 |
| 2 | Phil | 1964-05-23 | 2022-05-23 |
| 3 | Mark | 1948-12-10 | 1979-12-10 |
| 4 | Steven | 1991-02-28 | NULL |
| 5 | Helen | 1987-07-01 | 2050-07-01 |
+----+--------+------------+-------------+
5 rows in set (0.00 sec)
Is it possible to configure MySQL to return TIMESTAMP value as a UNIXTIMESTAMP by default, rather than casting every column in the SELECT statement?
MySQL has a function to convert a date to a unix timestamp.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219
You cannot do that in MySQL configuration.
You can do that on application level - e.g. in PHP, you can use the mysqli_result::fetch_fields() method to detect timestamp type and convert it, other connectors will have similar methods.
Or you can do it - as suggested - using UNIX_TIMESTAMP() function on timestamp columns.
It sounds as though you want a different view of the same data:
mysql> select * from t;
+------+---------------------+
| data | ts |
+------+---------------------+
| foo | 2013-03-19 16:54:45 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select data, unix_timestamp(ts) from t;
+------+--------------------+
| data | unix_timestamp(ts) |
+------+--------------------+
| foo | 1363712085 |
+------+--------------------+
1 row in set (0.00 sec)
mysql> create view tv (data, time_t) as select data, unix_timestamp(ts) from t;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tv;
+------+------------+
| data | time_t |
+------+------------+
| foo | 1363712085 |
+------+------------+
1 row in set (0.00 sec)
I'm, trying to calculate the number of days between two dates using ANSI SQL standard. But I'm missing something as this statement returns NULL in MySQL.
SELECT EXTRACT(DAY FROM DATE('2009-01-25') - DATE('2009-01-01')) AS day_diff;
I'm aware of the MySQL DATEDIFF function, but I'm curious why this code isn't working.
What am I missing?
Is this what you meant to do?
mysql> SELECT EXTRACT(DAY FROM DATE('2009-01-25')) -
EXTRACT(DAY FROM DATE('2009-01-01')) AS day_diff;
+----------+
| day_diff |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)
UPDATE:
If you want this to work for dates in different months (or even different years), then you can use the MySQL DATEDIFF() function.
Examples:
mysql> select datediff('2009-04-25','2009-01-01');
+-------------------------------------+
| datediff('2009-04-25','2009-01-01') |
+-------------------------------------+
| 114 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff('2010-04-25','2009-01-01');
+-------------------------------------+
| datediff('2010-04-25','2009-01-01') |
+-------------------------------------+
| 479 |
+-------------------------------------+
1 row in set (0.00 sec)