How to get month from timestamp value? - mysql

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

Related

Mysql query: filtering records MM from YYYYMMDD

I want to filter records based on the month of the available due date.
I am trying with the following query but It's not working.
SELECT * FROM m_bills WHERE due_date LIKE substr('201704',4,2)
in this example, I just want to filter all records with the month of April "04".
Use Query Like this select * from m_bills where monthname(due_date) = 'April'
monthname function

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'

MYSQL Query to retrieve current month data as well as check for last month

I have a table work_history('id','name','work','work_month','work_year').
I need a query to current current month data, but if data is not exist for current month, in this case retrieve last month data so on.....till then data not found.
nly
For Ex- If data found for 11-2012 so retrieve only this data, no need to check for previous month, but if not found so check for last month 10-2012. If again not found check 09-2012.
Thanks in advance
The month that you want is the maximum month that is available in the data. it must be either the current month or the closest to it. so such query should look like:
select ......
from work_history
where work_month = (select max(work_month) from work_history)
note that if the column work_month is a char column, you might need to convert the data to date type in order to allow correct comparison. based on the format seen in the example above:
select ......
from work_history
where str_to_date(work_month,'%m-%Y') = (select max(str_to_date(work_month,'%m-%Y')) from work_history)
EDIT
following all comments, the following query should fix the mistake I had and also give data based on ID of the user:
select wh.*
from work_history wh,
(select id, max(str_to_date(work_month,'%m-%Y')) as mx_month
from work_history
where str_to_date(work_month,'%m-%Y') <= curdate()
group by id
) wh2
where wh.id=wh2.id
and str_to_date(wh.work_month,'%m-%Y') = wh2.mx_month

Fetch month and day value from date attribute of table in mysql

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

how to get mysql rows on month basis

I am in a problem and i am confused. I have a table for events and in it there is a column that holds the timestamp of the date time when it was created. I want to get all events(rows) that were created in this month, the last month, and 2 more month before the last month. So, basically 4 months. I want to show them separately so, i can think i can query the datebase for all rows in this month in one query and another query for previous month and so on for 4 months. The problem is i don't know how can i do that. There is a month() function in mysql but i am lost, no idea how to use it.
Any help would be very much appreciated. Thanks
Edit: table structure is like this.
Title | Uri | created
Event | Event| 1337782223
name | uri |
.......
EDIT 2
Thanks all for your help..
I tried it myself and with my friend google...:)
I used this
SELECT * FROM `events` WHERE MONTH(FROM_UNIXTIME(created)) = $month;
and this seems to work for me, i will just pass the month whos rows i want from php thats easier for me...lol
Thanks again to all for your answers its appreciated. This place is awesome
Select * From your_table Group By Month(column_with_date);
Of couse you have to have a timestamp column in your table.
If not then it's impossible to restore the informations when the entry was inserted.
$month = '5,4,3,2';
select colum_name from table_name where month(table_field_name) IN ($thismonth) Group By month(table_field_name)
try this one if you are using separate query for each month
$todayDate = date("Y-m-d");
$thismonth = strtotime($todayDate);
$previous_month = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
and you can get past months just do minus NUMBER ex -2,-3 .....for current month
select colum_name from table_name where month(table_field_name) = month ($thismonth) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name);
for previous month
select colum_name from table_name where month(table_field_name) = month ($previous_month ) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name)