Select specific data from date field - php

I am trying to get some record from table for specific month and year and I have only complete date field.
In the above image I want to select the data from date but only for (2015-09)
E.g.:
SELECT * FROM attendance WHERE date = (year-month)

You can use year and month function:
SELECT * FROM attendance WHERE year(`date`) = '2015' and month(`date`) = '9'

You can set your date range from the beginning and end of that month.
SELECT *
FROM attendance
WHERE `date` BETWEEN '2015-09-01' AND '2015-09-30'

Try to use DATE_FORMAT.
Your query is:
SELECT * FROM attendance WHERE DATE_FORMAT(date,'%Y-%m') = '2016-05'

Related

Get current week, month and year records using SQL

I have a table having a column which has values like 2017-08-21
See the screencast here.
Now I need to get the values of current week, current month and current year.
How can I achieve this?
Current week :
SELECT *
FROM your_table
WHERE YEARWEEK(`your_date`) = YEARWEEK(CURDATE())
Current month
SELECT *
FROM your_table
WHERE YEAR(`your_date`) = YEAR(CURDATE())
AND MONTH(`your_date`) = MONTH(CURDATE())
Current year
SELECT *
FROM your_table
WHERE YEAR(`your_date`) = YEAR(CURDATE())
To select all records in current year
Select *
FROM `table_name`
WHERE YEAR('hit_date') = YEAR(CURDATE());
To select all records in current month
Select *
FROM `table_name`
WHERE MONTH('hit_date') = MONTH(CURDATE())
AND YEAR('hit_date') = YEAR(CURDATE());
To select all records in current week
Select *
FROM `table_name`
WHERE YEARWEEK('hit_date') = YEARWEEK(CURDATE());
Please note regarding YEARWEEK you can specify your week start day depend on mode ( second parameter ) please have a look MySQL YEARWEEK() Function

PHP Mysql SELECT Last month and user selected month data

I want to retrieve last month data and my combo-box selected month date
this is my code and this query is not working. i think the problem is in INTERVEL command.
Please help me to use INTERVAL 1 MONTH correctly.
$result = mysql_query("SELECT manufacturer,model, SUM(replace(payment_one,',','')) as payment_one FROM table1 WHERE
YEAR(payment_one_date) = '$YYYY'- INTERVAL 1 MONTH AND
MONTH(payment_one_date) = '$MM'-INTERVAL 1 MONTH AND
payment_one_bank='CB'");
SELECT
manufacturer,
model,
SUM(replace(payment_one,',','')) payment_one
FROM table1
WHERE (
payment_one_date < '$YYYY-$MM-01 00:00:00' /* <-- can be incorrect for your field type */
AND payment_one_date >= ('$YYYY-$MM-01 00:00:00' - INTERVAL 1 MONTH)
) AND payment_one_bank='CB'

Mysql Multiple queries to calculate values for month

first of all i want to extract dates of the relevant month from the database.for that i have used following query and i had generated the following output.
code
SELECT DISTINCT date as tot FROM attendance WHERE date LIKE '%2016-06%'
output
then i need to get the present employees for that dates from the database.but when i try that, it gives me total for one date only.how can i over come this.Here is my code.
SELECT DISTINCT date as days, COUNT(DISTINCT employee_id) as tot FROM attendance WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
You need to use group by. mysql just returns an arbitrary value when you omit fields from the select list that are not included in aggregate functions. Then you can remove distinct:
SELECT date as days, COUNT(DISTINCT employee_id) as tot
FROM attendance
WHERE in_time != '' AND out_time != '' AND date LIKE '%2016-06%'
GROUP BY date
Per comment, what data type is your date field? Assuming it's a date, you could remove like and use year and month:
AND Month(date) = 6 AND Year(date) = 2016

MySQL - Select the least day of the current week, not necessarily the first day of the week

Using PHP/MySQL
I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.
My date column is named 'theDate' and the datatype is 'DATE'
The query would need to be something like:
SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1
It would return a single row of data.
This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:
SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;
This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):
SELECT *
FROM table_name
WHERE playerName = '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
Without trying this, you probably need an inner query:
select *
from table_name tn
where tn.the_date =
(select min(the_date)
from table_name
where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
and YEAR(the_date) = YEAR(CURDATE()))
viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.
Try this
SELECT * FROM table_name WHERE name = '$username'
AND your_data IS NOT NULL
AND WEEK(the_date,0 = WEEK(NOW(),0))
ORDER BY DATE_FORMAT(the_date,'%w') ASC
Try the following, replace YOUR_DATE with the date from the column you want (theDate):
SELECT ADDDATE(YOUR_DATE, INTERVAL 1-DAYOFWEEK(YOUR_DATE) DAY)
FirstDay from dual
Did you try:
SELECT ADDDATE(theDate , INTERVAL 1-DAYOFWEEK(theDate ) DAY) FirstDay
FROM table_name
WHERE playerName = '$username'
ORDER BY theDate DESC
LIMIT 1

MySQL How to SELECT data from table which recorded today?

Use PHP and MySQL. In my table, there is date field (datetime) recorded by NOW() sql function. Example value of data in this field is 2010-10-07 10:57:36. How can I SELECT all data which day-month-year is today. I try to use code as below:
SELECT * FROM table WHERE date=????
Try this:
SELECT * FROM table WHERE date > CURDATE();
CURDATE() will return the current date as 2011-10-07 which will be cast to 2011-10-07 00:00:00 when comparing datetimes to it.
Note that if you use DATE(date) = CURDATE() you will run a date conversion for every row in the table, which will be really bad for your perfomance if you have many rows and/or you need to run the query often. Also make sure you have an index on date, otherwise both methods will be even slower.
SELECT * FROM table where DATE(date)=CURDATE()
SELECT * FROM tableName WHERE DATE(fieldDate) = DATE(NOW());
The date_format function allows you to easily switch between various granularities:
Select everything from the same day:
select * from table
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');
From the same month:
select * from table
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');
From the same year:
select * from table
where date_format(date, '%Y') = date_format(now(), '%Y');
From the same hour:
select * from table
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');
and so on.
Try this
SELECT * FROM table WHERE DATE(my_date)=DATE(now())
my_date -> column name
SET #day = '2017-12-12' ;
SELECT * FROM table WHERE dateColumn BETWEEN DATE(#day) AND DATE_ADD(DATE(#day), INTERVAL 1 DAY ) ;
use something like this it exactly works on my code(access database):
select * from Table t where t.column>=Date() and t.column< Date() + 1
use between.
select * from table date between '2010-10-06' and '2010-10-08';

Categories