perkeyone
something clever
- Reaction score
- 71
i am having trouble sorting some information from my database.
at one point in time when i ran my query in php the records came in a different order than when i ran the same query on mysql workbench.
basically my problem is that i want to sort some info before aggregating it.
the database has two schemata:
- coursecatalog, which contains general info about courses being taught,
- mastercalendar, which contains specific info about each offering of the courses
coursecatalog has the following tables:
- supercourse, which has an id, a name, and a description of a course or courses
- course, which has a course number and is associated with a super course id
mastercalendar has the following tables:
- offering, which has a section number and is associated with a course number
- date, which has a date and time and is associated with a section number
so... a super course may be associated with multiple courses,
an offering may be associated with multiple dates and times,
and a course may be associated with multiple offerings.
what i want to do, is run a query, which will return a list of...
the supercourse name,
the course number,
the section number,
and a concatenated list of all associated dates (aggregate, sorted)
for each offering in the database
ordered by the earliest date (sorted)
this is a simplified version of the query i am using,
i renamed the columns to (hopefully) make it easier to understand.
it retrieves all the pertinent information but does not sort the list of dates
this is my unedited query, which is uglier and has stupid column names.
i am only including this incase my error is specific to this query.
at one point in time when i ran my query in php the records came in a different order than when i ran the same query on mysql workbench.
basically my problem is that i want to sort some info before aggregating it.
the database has two schemata:
- coursecatalog, which contains general info about courses being taught,
- mastercalendar, which contains specific info about each offering of the courses
coursecatalog has the following tables:
- supercourse, which has an id, a name, and a description of a course or courses
- course, which has a course number and is associated with a super course id
mastercalendar has the following tables:
- offering, which has a section number and is associated with a course number
- date, which has a date and time and is associated with a section number
so... a super course may be associated with multiple courses,
an offering may be associated with multiple dates and times,
and a course may be associated with multiple offerings.
what i want to do, is run a query, which will return a list of...
the supercourse name,
the course number,
the section number,
and a concatenated list of all associated dates (aggregate, sorted)
for each offering in the database
ordered by the earliest date (sorted)
this is a simplified version of the query i am using,
i renamed the columns to (hopefully) make it easier to understand.
it retrieves all the pertinent information but does not sort the list of dates
Code:
SELECT
mc_o_id,
mc_o_course_number,
mc_o_section_number,
GROUP_CONCAT(DISTINCT sd SEPARATOR '<br/>') AS meeting,
cc_s_name
FROM mastercalendar.offering
JOIN coursecatalog.course ON mc_o_course_number=cc_c_course_number
JOIN coursecatalog.supercourse ON cc_c_cc_s_id=cc_s_id
JOIN (
SELECT
mc_d_mc_o_id AS tid,
mc_d_date_start AS sd,
mc_d_time_start AS st,
FROM
mastercalendar.date
ORDER BY sd ASC,st ASC) AS temp ON tid=mc_o_id
GROUP BY mc_o_id, mc_o_course_number, cc_s_name
ORDER BY sd ASC,st ASC
i am only including this incase my error is specific to this query.
Code:
SELECT
mcr_id,
mcr_course_number,
mcr_course_section,
mcr_year,
GROUP_CONCAT(DISTINCT dt SEPARATOR '<br/>') AS meeting,
ccr_name
FROM travisb_mastercalendar.record
JOIN travisb_coursecatalog.course ON mcr_course_number=ccc_course_number
JOIN travisb_coursecatalog.record ON ccc_ccr_id=ccr_id
JOIN travisb_mastercalendar.record_rule ON mcrr_mcr_id=mcr_id
JOIN (
SELECT
mcrd_mcr_id AS tid,
mcrd_date_start AS sd,
mcrd_time_start AS st,
CONCAT_WS(' - ',
CONCAT_WS(' ',DATE_FORMAT(mcrd_date_start,'%m/%d/%y'), DATE_FORMAT(mcrd_time_start,'%h:%i %p')),
CONCAT_WS(' ',
IF(mcrd_date_start=mcrd_date_end,'',DATE_FORMAT(mcrd_date_end,'%m/%d/%y')), DATE_FORMAT(mcrd_time_end,'%h:%i %p'))) AS dt
FROM
travisb_mastercalendar.record_date
ORDER BY sd ASC,st ASC) AS temp ON tid=mcr_id
WHERE mcr_id IN(
SELECT DISTINCT
mcrd_mcr_id
FROM travisb_mastercalendar.record_date
WHERE
mcrr_course_type=$_POST[type] AND
mcrr_region=$_POST[region] AND
(DATE_FORMAT(mcrd_date_start,'%Y')=$_POST[year] OR
DATE_FORMAT(mcrd_date_end,'%Y')=$_POST[year]) AND
(DATE_FORMAT(mcrd_date_start,'%m')=$_POST[month] OR
DATE_FORMAT(mcrd_date_end,'%m')=$_POST[month]))
GROUP BY mcr_id, mcr_course_number, ccr_name
ORDER BY sd ASC,st ASC