I have created a table as below.
actor(id, first_name, last_name, dob);
Where dob is in Y-m-d format. So I would like to fetch actors, who they have b'days for perticluar month or perticcular day. Please help me to get the solution.
Use this query for date
select * from actor where DAY(dob) = 12;
And for month
select * from actor where MONTH(dob) = 5;
You can combine both of them as well using AND operator.
Have a look at this. You will find more date and time functions that you can use
Try day and month functions
select * from actor
where day(dob)=x and month(dob)=y
See more datetime functions here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Related
I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())
There is table called payment and timestamp is column which has data like 1462399200.I want to extract records of specific month from this database table. For example I want all records of May month.Here is the code I am using, but it isn't yielding me any result.
$sql="SELECT * FROM payment WHERE monthname(timestamp)=5";
$result=mysqli_query($db,$sql);
$counta=mysqli_num_rows($result);
You could use this
SELECT *
FROM payment
WHERE MONTH(FROM_UNIXTIME(timestamp)) = MONTH(CURDATE())
AND YEAR(FROM_UNIXTIME(timestamp)) = YEAR(CURDATE());
To get all values for this month.
SELECT *
FROM payment
WHERE MONTH(FROM_UNIXTIME(timestamp)) = 1
AND YEAR(FROM_UNIXTIME(timestamp)) = 2016;
And the second option to get all values for january 2016
Please let me know if this is helpfull.
You need to use below code:-
mysql> SELECT MONTH(FROM_UNIXTIME(1462399200));
MYSQL Convert timestamp to Month
I have also mentioned in comment
I hav the next query in SQL:
SELECT id FROM student WHERE DATE LIKE '%$inputdate'
the inputDate is in the next format: 2010.08.03
I also want to change the date of the date, by increasing the day in a week (+7).
I try this:
SELECT id
FROM student
WHERE DATE LIKE '%$inputdate'
OR DATE Like '%CAST(RIGHT("2012-11-03", 2)AS INT) + 7;
But it gives me SQL syntax error. How can I write it correctly?
since you are using MySQL, make use of DATE() function
SELECT id
FROM student
WHERE DATE(`DATE`) = DATE(inputdate) OR
DATE(`DATE`) = DATE(DATE_ADD(DATE(inputdate),INTERVAL 7 DAY))
SOURCES
DATE()
DATE_ADD()
I have mysql database and is full of over 2 years of data. How can I make a query in a way that it will get the result as below?:
January
Week 1
...
data rows here
....
Week 2
...
...
Week 3
...
...
Week 4
...
...
February (same as above)
These are the field structures:
- date (in yyyy-mm-dd format)
- job (integer autoincrement)
- person (varchar)
Try this, it will generate output pretty similar to one required by you
SELECT MONTH(date) AS MONTH, WEEK(date) AS WEEK, DATE_FORMAT(date, %Y-%m-%d) AS DATE, job AS JOB, person AS PERSON GROUP BY WEEK(date);
GROUP BY year(record_date), MONTH(record_date)
Check out the date and time functions in MySQL.
It has to be a query, or you can use ajax too? Using ajax you could do a loop in javascript:
date = [initial date]
while date <= [final date] {
divUpdate = 'divrecs'; // will be used in updateDiv function
url = 'getrecs.php?date1='+date+'&date2='+(date+6);
conecta(url,updateDiv);
date = date+7;
}
And inside getrecs your query would be:
$stmt = $dbh->prepare("select * from yourtable where date >= '".$_GET['date1']."' and date <= '".$_GET['date2']."'");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
echo "Do anything with your data here: $row[1] etc<BR>";
}
}
Hope that helps!
SELECT
MONTH(created_datetime) AS month,
WEEK(created_datetime) AS week,
DATE(created_datetime) AS date,
COUNT(id) AS count
FROM users
WHERE date(created_datetime) BETWEEN '2017-05-01' AND '2017-12-31'
GROUP BY month
ORDER BY created_datetime
If you want records date wise that you need to use GROUP BY date.
If you want records weekly that you need to use GROUP BY week.
If you want records monthly that you need to use GROUP BY month.
How can I get records from my table using month/year? I have a table like this:
Name - varchar
DueDate -datetime
Status -boll
DueDate is project due date, I want record corresponding to month/year, not full date, I mean record for specific month.
How can I do this in mysql?
Simply use MONTH() and YEAR():
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
You could use a between statement:
select * from Project
where DueDate between '2010-01-01' and '2010-02-01'
Do not use this here recommended solution with MONTH() and YEAR(). It prevents MySQL to use index on column DueDate if you have one and it forces MySQL to examine all rows in table.
Instead, use BETWEEN clause like this:
SELECT * FROM Project
WHERE DueDate BETWEEN '2010-01-01' AND '2010-02-01'
Or another solution with IN clause:
SELECT * FROM Project
WHERE DueDate IN ('2010-01-01', '2010-01-02', '2010-01-03', ..., '2010-01-31')
This two solutions allows MySQL to use index, so performance will be better.
SELECT Name FROM Table Where MONTH(datetime) = 1;
1 = January.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
it will select current year's specific month
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = YEAR(NOW())
If you input month format "Y-m" you can use:
SELECT * FROM Project WHERE DATE_FORMAT(DueDate,"%Y-%m") = '2010-01'
You can extract the MONTH() and YEAR() for your DueDate.
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
SELECT * FROM TABLE WHERE DueDate LIKE '2020-01%';
use in case you can substring your date data.