SQL: Functions
November 26, 2020
Examples:
ADDDATE(birtday, INTERVAL 1 YEAR)
// Will return a new column in which values will be equled to the "birthday" column value + 1 year
SELECT * FROM users WHERE birthday < SUBDATE(NOW(), INTERVAL 16 YEAR);
// Will select users who are 16 years old at least
SELECT *, DATEDIFF(CURDATE(), birtday) FROM users;
// Returns a new column containing the interval of days between the current date and the "birtday" column value for each line. -> Output: "857" (for: 857 days)
SELECT *, FROM_DAYS(DATEDIFF(CURDATE(), birtday)) FROM users;
// Output: "0002-04-07" (for: 2 years, 4 months and 7 days. Which is equivalent to 857 days)
SELECT *, DATE_FORMAT(birthday, "%W %d %M %Y") FROM users;
// Return a new columns containing "birthday" column values with this new format: "Saturday 27 Octiber 2020".
SELECT COUNT(*) AS population,
YEAR(birthday) AS year,
MONTH(birthday) AS month
FROM users
GROUP BY
YEAR(birthday),
MONTH(birthday)
// Will return a table as following:
population year month
4 1980 3
1 1981 5
2 1982 1