Get records of current month [duplicate] - mysql

This question already has answers here:
Select current months records mysql from timestamp column
(12 answers)
How do I select between the 1st day of the current month and current day in MySQL?
(17 answers)
Closed 9 years ago.
How can I select Current Month records from a table of MySql database??
Like now current month is January. I would like to get records of January Month, Where data type of my table column is timestamp.I would like to know the sql query.
Thanks

This query should work for you:
SELECT *
FROM table
WHERE MONTH(columnName) = MONTH(CURRENT_DATE())
AND YEAR(columnName) = YEAR(CURRENT_DATE())

Check the MySQL Datetime Functions:
Try this:
SELECT *
FROM tableA
WHERE YEAR(columnName) = YEAR(CURRENT_DATE()) AND
MONTH(columnName) = MONTH(CURRENT_DATE());

Try this query:
SELECT *
FROM table
WHERE MONTH(FROM_UNIXTIME(columnName))= MONTH(CURDATE())

Related

Mysql Query data between date on joined table [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
SQL date format convert? [dd.mm.yy to YYYY-MM-DD]
(3 answers)
Closed 5 years ago.
Am trying to query data from two joined tables with a date as criteria using the query below but it brings nothing, can someone please show me what I did wrong.
SELECT Journal_T.GL_ID as Akaunti
,coa_t.gl_name_vc
,SUM(Amount_NU) as YTD
FROM Journal_T
JOIN coa_t
ON journal_t.gl_id = coa_t.gl_id
WHERE CAST(date_dt AS DATE) BETWEEN '25/08/2017' AND '25/08/2017'
AND bs_category_vc='Rev'
GROUP BY coa_t.gl_name_vc
,Journal_T.GL_ID
ISO 8601 Date format would be '2017-08-26'. Try this query to see default Date format on your server:
select STR_TO_DATE("26/08/2017", '%d/%m/%Y')
Then you can convert it, for example to '%Y-%m-%d' format:
DATE_FORMAT(STR_TO_DATE("26/08/2017", '%d/%m/%Y'), '%Y-%m-%d')
I think you can try out this query :
SELECT Journal_T.GL_ID as Akaunti,coa_t.gl_name_vc,sum(Amount_NU) as YTD
FROM Journal_T
LEFT JOIN coa_t on journal_t.gl_id = coa_t.gl_id
WHERE DATE_FORMAT(date_dt,'%d-%m-%Y') BETWEEN '25/08/2017' AND '25/08/2017'
AND bs_category_vc='Rev'
GROUP by coa_t.gl_name_vc,Journal_T.GL_ID

mysql select the last value of a day for 7 days [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have a table with date and Etotalday.
What I want to select is the last value of the last 7 days.
Many solutions I looked for gives the highest value of a day which is not always the same as the last value.
In my case table is build with values and the day starts with the highest value of the day before and at about 06.00 new values are added.
Is there somebody who can gives me a hint how to do this?
thnks
Lookes like duplicate question but I want the last value not the highest value and not the max value.
You can do this with order by and limit
select t.*
from PacData t
where date >= now() - interval 7 day
order by date;
limit 1;
If you want to return the last value for each of the past seven days (which might be the intent of the question), then here is one method:
select t.*
from PacData t
where not exists (select 1
from PacData t2
where date(t2.date) = date(t.date) and t2.date > t.date
);
This says: "Get me all records from the table where there is not record on the same date with a larger value from the date time column". By the way, date is a lousy name for a column that stores both a date and a time.

MySQL select Display Zero when group by month or week [duplicate]

This question already has answers here:
Mysql to select month-wise record even if data not exist
(2 answers)
Closed 9 years ago.
I have a table with a column date and a column ID (to keep it simple). I am doing a query to count the total and group by week
SELECT MONTH(table1.Date_1st), YEAR(table1.Date_1st), COUNT(table1.Id)
FROM table1 as table1
WHERE Date_1st BETWEEN '2013-04-01' AND '2013-07-25'
GROUP BY WEEK(Date_1st)
the problem is there is no results on specific week the result is not taken into consideration. I already try ifnull(table1.Id,0)
Try ISNULL or other NULL SQL functions:
NULL functions
ISNULL returns a 0 if the value is null.

MYSQL to display only the earliest of duplicates [duplicate]

This question already has answers here:
Find duplicate records in MySQL
(28 answers)
Closed 8 years ago.
select name, reported_at from nodes
where reported_at < curdate() or reported_at is null
group by name
Output:
name reported at
ncs-linux-test.edu 2012-03-16 18:36:03
ocdev1.net 2012-04-06 16:32:02
pinc-ctm.net NULL
With that statement, I get any results form reported at/name that are less than the current date.
What I need though is the statement to only pull out data that has a duplicate(s) with more current information.
For example:
The statement would only pull out:
ncs-linux-test.edu 2012-03-16
if there was an
ncs-linux-test.edu
with a date more current than 2012-03-16.
select *
from nodes n
join nodes nlater
on n.name = nlater.name
and n.reportedat < nlater.reportedat

Mysql date problem [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Select by Month of a field
i want to get the data from database whose month is 7. means
My Date format is
PostDate datetime
now i want to query..
select * from tabelname where PostDate = 7
If you are asking how to query MySQL for date objects based on month, then this may be your answer.
select * from tablename where month(PostDate) = '07';
Keep in mind this function (month()) is MySQL specific.