Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMayur Pande
Courses Plus Student 11,711 PointsSelecting count from table with similar values
I want to be able to select the count of a table in mysql which could have the same email address but different start times.
So for example I have a table called driverdetails
| driveremail | studentemail | tutoremail | starttime | endtime | location | class |
| -------------- | ------------ | --------------- | ------------------- | ------------------ | -------- | ----- |
| Y.S@southls.com | me@me.com | m.g@southls.com | 2016-05-25 17:00:00 | 2016-05-25 18:00:00 | Brixton | Maths |
| Y.S@southls.com | me@me.com | m.g@southls.com | 2016-05-26 10:00:00 | 2016-05-26 11:00:00 | Brixton | Maths |
I am trying to write a query with a left inner join that gives me the result for only one of these according to the difference in time. I wrote a function earlier that passed in the hidden values for the driveremail, starttime, and tutoremail. However this did not work when passing the count result to my twig view. Is there a way I can do this using join?
2 Answers
jcorum
71,830 PointsMayur, you can't do a join unless you have more than one table, and you mention only one.
If you just need count of the records from this one table for a particular driver with different start times then this should do it:
SELECT COUNT(*) FROM driverdetails WHERE driveremail = '...'
If you need to just get records with a given difference in time, then you could do something like this:
SELECT COUNT(*) FROM driverdetails WHERE driveremail = '...' AND DateDiff(hh, starttime, endtime)
For details on DateDiff see: http://www.w3schools.com/sql/func_datediff.asp
Seth Kroger
56,413 PointsI think you should take a closer look at what mysqli_query actually returns. Especially in regards to this bit in intval's docs: "intval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1."
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsHi,
Thanks for the reply. Sorry I wasn't being clear in my post. I do have another table.
Initially I did try to do what you have said by using a where clause. However when passing back the count value to my controller nothing happened.
For example I had a stripe button that when some clicks and pays it passes the variables for driveremail,tutoremail,starttime to the function for get_student_driver_count
then my get_student_driver_count fn deals with getting count
This was all fine, but then I ran into issues with my controller
This did not work for me unfortunately. So I thought maybe there is a way of simply creating a function which simply returns the value of the driverdetails table where the driveremail, and starttime are the same. So if I have two different entries like in my table above that have the same driveremail, but different starttimes it will only return a count of 1.
Do you think this is possible?