Jump to content

Returning the Correct ID with column groups and...


AnalyzeThis

Recommended Posts

I am trying to return the correct ID, the persons name, their , their most recent date taking a tanning course, for each type of "tanning course" ;) . What I have below works only when the tanning courses are entered in correct order. This is not always the case however. What I think I need is something like WHERE `ID` =(SELECT MAX(...) FROM ...). But am really unsure how to go about it due to the "complex" statement.

 

SELECT `tanning_courses`.`name` AS `course_name`,max(`tanning_attendance_records`.`ID`) AS 'cnid', `tanning_attendance_records`.`group`, `tanning_attendance_records`.`name` AS `person_name`, DATE_FORMAT(max(`date`), '%m/%d/%Y' ) AS `date`,

IF(`tanning_courses`.`name` LIKE '%VEGITABLE%', DATE_ADD(max(`date`),INTERVAL 10 YEAR), IF(`tanning_courses`.`name` LIKE '%FRUIT%', DATE_ADD(max(`date`),INTERVAL 30 YEAR), IF(`tanning_courses`.`name` LIKE '%GRAIN%', DATE_ADD(max(`date`),INTERVAL 20 YEAR), '?'))) AS `expiration`

FROM `tanning_attendance_records`

LEFT JOIN `tanning_courses` ON `tanning_attendance_records`.`course` = `tanning_courses`.`ID`

WHERE `group` = '{$client["ID"]}' AND passed = 1 GROUP BY `person_name`, `course_name` ORDER BY `expiration` ASC LIMIT {$set_limit}, {$limit}"

 

 

 

Thank you in advance for any assisstance!

Link to comment
Share on other sites

tanning_attendance_records

=====================================

ID (Primary Key) | name | group | course | date | etc.

 

tanning_courses

=====================================

ID (Primary Key) | name | description | title | etc.

 

Person attends a course on a date. The course is only valid for a certain amount of time (VEGITABLE 10 years, GRAIN 20, FRUIT 30 years). I want to return the most recent of each type of course taken by the same person and let them know when that course is going to expire and have it ordered to show that the most recent expiration dates. Because the course will be titled "GRAIN VTE 100" or "GRAIN VVT 100" there is a if statement. Please let me know if you need anything else.

 

Thanks in advance.

Link to comment
Share on other sites

You have logic such that

IF(`tanning_courses`.`name` LIKE '%VEGITABLE%', DATE_ADD(max(`date`),INTERVAL 10 YEAR)

 

Are there different courses with the name 'VEGITABLE' in them? You state

I want to return the most recent of each type of course taken by the same person . . .

 

That won't work since a user *could* take two courses of the same type. Your GROUP BY is only based upon the user and the course name.

Link to comment
Share on other sites

Each course refered in tanning_attendance_records refers to a id of a unique course. Each uniquie course contains a name which may contain either the words "GRAIN", "VEGITABLE", or "FRUIT". If it does contain those words then they have predermined expirations dates.

 

I want to return only the most recent GRAIN, VEGITABLE, and FRUIT for each users and all the unclassified ('?'). If they have the words GRAIN, VEGITABLE, FRUIT then they have a spefific expiration date.

 

I could also rework the tanning_courses database to feature a `expiration_date` field, which is making more sense now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.