Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySql Query rows for this year

Posted on 2014-12-21
9
Medium Priority
?
55 Views
Last Modified: 2016-05-25
I have a query that i need to get only the rows that are for the current year.

The column that has the unix time is referrals.entered

here is the current query:

SELECT referrals.*, to_person.fname as to_fname, to_person.lname as to_lname, from_person.fname as from_fname, from_person.lname as from_lname FROM referrals LEFT JOIN person as from_person ON (from_person.ID = referrals.from_ID) LEFT JOIN person as to_person ON (to_person.ID = referrals.to_ID) LEFT JOIN chapter_relation as from_relation ON (from_person.ID = from_relation.person_ID) LEFT JOIN chapter_relation as to_relation ON (to_person.ID = to_relation.person_ID) WHERE from_relation.chapter_ID = to_relation.chapter_ID AND from_relation.chapter_ID = '$chapterid' ORDER BY referrals.entered DESC
0
Comment
Question by:jporter80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40512250
There is nothing in your query where you filter on year so

WHERE
....
YEAR(referrals.entered)=2014
....

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40512252
Or did you mean for this to be a generic sql for whatever the current year is?

WHERE
....
YEAR(referrals.entered)=YEAR(CURDATE())
....

Open in new window

0
 

Accepted Solution

by:
jporter80 earned 0 total points
ID: 40512259
Well maybe i answered my own question... but i just did it this way unless you think it could be better:

$currentyear = date('Y');
$startyear = strtotime("01 January ".$currentyear);
$endyear = strtotime("31 December ".$currentyear);

$numberreferrals = mysqli_query($mysqliglobal,"SELECT referrals.*, to_person.fname as to_fname, to_person.lname as to_lname, from_person.fname as from_fname, from_person.lname as from_lname FROM referrals LEFT JOIN person as from_person ON (from_person.ID = referrals.from_ID) LEFT JOIN person as to_person ON (to_person.ID = referrals.to_ID) LEFT JOIN chapter_relation as from_relation ON (from_person.ID = from_relation.person_ID) LEFT JOIN chapter_relation as to_relation ON (to_person.ID = to_relation.person_ID) WHERE from_relation.chapter_ID = to_relation.chapter_ID AND from_relation.chapter_ID = '$chapterid' AND referrals.entered > $startyear AND referrals.entered < $endyear ORDER BY referrals.entered DESC");
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 58

Expert Comment

by:Gary
ID: 40512263
I think it's easier going the sql route, and just pass in the year from php using
$year = date("Y");
...
WHERE
....
YEAR(referrals.entered)=$year
....

Open in new window



Doing a date > and date < is just wasted code.
Plus the way you have it it would be
year > 2014 and year < 2014 = nothing
0
 

Author Comment

by:jporter80
ID: 40512265
im using unix time so the variables declared before query are getting unix time of the beginning of the current year and the end of the current year. Now im querying everything in between with > or <
0
 
LVL 58

Expert Comment

by:Gary
ID: 40512268
Ahh true - thought you were querying against the year, still it would be easier and less code to just query on the year as exampled here http:#a40512263

edit.
Your code is still > than the 1st of Jan and < 31st Dec which means the 2nd Jan til the 30th Dec
Should >= and <=
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40512269
Just a reminder: use >= and <= otherwise you'll miss the data from the first and the last day of the year!
0
 

Author Comment

by:jporter80
ID: 40512274
ahhh.. thanks for the catch
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question