MySQL query four years ago - php

I happen to have ordered some data for years, I have a column over the years since the dates are not updated in the records. How could inquire to get me records only four years ago?
I tried this way:
$year=date ("Y"); //get the current year
$cons="SELECT iden,
ano,
wh,
so
FROM content WHERE wh='n'
AND ano BETWEEN 2013 AND".$year ."
ORDER BY ano desc, id DESC";
But the problem is that next year I have to modify the query to put 2014 instead of 2013. Had way to mount a conditional subtraction do?
Ano column I have: 2016, 2015, 2014, 2013, etc.
Let's see if I can lend a hand, because I have little knowledge of SQL and would like to learn more. Thank you.

Could you not just subtract 4 from your current year and use that as the first year?
$previous_year=date("Y")-4;
"...BETWEEN ".$previous_year." and ".$year."...."

Related

php week(curdate()) sum of other years

OK so i have this query:
SELECT obrero as MAESTRO, sum(costo_semanal) AS TOTAL_COST,
ROUND(SUM(week_cost)/MONTH(CURDATE()),2) AS MONTHLY_COST,
ROUND(SUM(week_cost)/WEEK(CURDATE()),2) AS WEEKLY_COST
from tbl_costos WHERE obrero ='$maestro'
I did this and it worked great in 2015, the problem is that now on 2016 we go back to week 1 and month 1 so im not having the proper division.
What I need to accomplish is to sum the 52 weeks of the past year and sum the current week of this year so i could have a % of the cost per week
"cost/number of weeks" = $cost per week.
for example of today 2016-01-18 being the 4th week of the year
total paid (of 2015 and 4 weeks of 2016) = $4000.00
weeks = 52 + 4 = 56
4000.00/56 = $71.4285714 average cost per week
The same thing applies to Months, it should be doing the division with 13, and cus January is month 1, its doing it over 1.
I could just do:
SUM(week_cost)/(12+ MONTH(CURDATE()));
and
SUM(week_cost)/(52 + WEEK(CURDATE()));
but that would solve the problem for this year only!!
You should use DATEDIFF() in order to calculate the number of days each maestro has worked from a given date, "start_date" to a given end date "end_date", and then convert those days to weeks, months and years if you need to.
Be flexible and anticipate that each maestro can work from any given date, or a report can be asked from any given date. Your's assume that each maestro started to work on 2015-01-01 and that all reports shall be done with this in mind. Reality is different.
Select obrero as MAESTRO, sum(costo_semanal) as TOTAL_COST, ROUND(SUM(costo_semanal/ROUND(DATEDIFF(start_date, end_date)/30,0)),0) AS MONTHLY_COST, ROUND(SUM(costo_semanal/ROUND(DATEDIFF(start_date, end_date)/7,0)),0) AS WEEKLY_COST FROM tbl_costos WHERE obrero=$maestro;
Do not place the variable $maestro on your query, better use PREPARED STATEMENTS.

query for any record with timestamp for specified month and day, for any/all years

I have a mySQL table that stores the high temperatures for every day over a span of several years. When A given day's temp is logged to the database, a UNIX timestamp for midnight GMT time for that day is logged as well. I need to run a query that will return all the temps over the years and the date they're associated with for any user-selected day and month. So for example, if the use selects August 11th, I need the high temperature for 8/11/2003, 8/11/2004, 8/11/2005, etc (1060560000, 1092182400, 1123718400, respectively)... all records with a timestamp that will convert to that date.
I know I can generate the exact timestamps for that date for the years the data spans and use them in the query like this:
SELECT tStamp, high_temp FROM temps WHERE tStamp=1060560000 OR tStamp=1092182400 OR tStamp=1123718400;
but this obviously would not be ideal. In order to allow for an unlimited range of years, I'll need something that can find multiples or something to that effect... I think?
I tried (in PHP):
$in = $_POST['date']; // ex: 8/11
$mult = strtotime($in . '/1970 00:00:00');
$sql = "SELECT tStamp, high_temp FROM temps WHERE MOD(tStamp, $mult) = 0";
....
but this did not return any records for August 11th. I'm not sure where to go from here. Any insight would be greatly appreciated.

CakePHP - SQL Query to join 3 tables

i have 3 tables
News(id, year_id, month_id, title, description) where year_id and month_id are foreign keys from tables
years(id,...) and
months(id,...)
respectively.
2014 - 2015 - 2016 - 2017
January
news title 1 (January 1, 2014)
news title 2 (January 17, 2014)
February
news title 1 (February 5, 2014)
news title 2 (February 20, 2014)
As you can get an idea that first of all, i want to show all year's list(2013, 2014, 2015) and i did it successfully.
And now i want that below that list of years, the news must be displayed grouped according to months(e.g. all news of January month under "January" heading and so on till December of Year 2014 and below that, similarly complete set of all news according to months of Year 2015 and so on....)
I just want to know if there is any way to get required results from a SQL join or group by query
or i will have to manage this 100% using coding and by simply fetching all records from table without any query.
I have searched for this on google but not getting the solutions.
Need just a little idea regarding this.....
I may know nothing about CakePHP and I've been wondering why would you ever have separate tables with month and year ID's.. as I've asked in a comment which you didn't mind replying.
Anyway, if you want to keep your database structure, just use this :
SELECT
news.title,
months.name,
months.day,
years.year
FROM news
JOIN months ON months.id = news.month_id
JOIN years ON years.id = news.year_id
ORDER BY
months.month ASC,
years.year ASC
I think this should do ( did not check obviously if there's any error but.. this is the idea ).
You cannot group since we're not using any aggregate function.
To note that I just assumed the columns of years and months as I have no idea how did you name your other columns from those tables aside the id which I guess it's just an index but you can replace them as you wish
Even though I suggest getting rid of your months and years tables and just store the month and year number if you really want to in the News table as you can always output a month's name in PHP.

mysql count rows and group them by month [duplicate]

This question already has answers here:
MySQL Query GROUP BY day / month / year
(17 answers)
Closed 8 years ago.
I have a table called cc_calls and there I have many call records I want to count them and group them in months I have a timestamp called starttime and I can use that row to extract the month, also limit the count for 12 months
the results should be like:
Month Count
January 768768
February 876786
March 987979
April 765765
May 898797
June 876876
July 786575
August 765765
September 689787
October 765879
November 897989
December 876876
Can anyone guide me or show me the mysql query that I need to get this result.
SELECT
MONTH(starttime),
COUNT(*)
FROM cc_calls
GROUP BY MONTH(starttime)
Be aware though, that this counts for each month of every year. You might want to include the year in the select and group by clauses or filter for a specific year in the where clause.
SELECT
MONTH(starttime),
COUNT(*)
FROM cc_calls
GROUP BY (MONTH(FROM_UNIXTIME(starttime)))

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories