Jump to content

select according to particular order


Go to solution Solved by Barand,

Recommended Posts

I teach 6 different class groups, named "A" to "F", respectively.
 
I have a small homework database from which I want to display the most recent 1 item of homework (record) for the class groups I have on any particular day of the week.
 
So, for example, if it's Monday today, I want to display 5 items of homework, in order, for groups "A", "B", "C", "D" and "E".
 
On the other hand, if it's Thursday today, I want to display 3 items of homework, in the order "F", "E" and "D", as that is the way my class groups are ordered on Thursday.
 
I've created a switch statement and a select query as follows but the query isn't pulling the records out as I want them. Can anyone tell me what I'm doing wrong in the select query? How should I combine the auto-incrementing id column and the classgroups column so that the records are displayed as I want them?...
 
(Right now, Tuesday, it's displaying D, C, B, A but it should be displaying A, B, C, D…)
 
TIA
 
switch (date("l")) {
    case 'Monday':
        $ord =  "`id` DESC, FIELD(`classgroup`, 'A', 'B', 'C', 'D', 'E')";
        $lim = 5;
        break;
    case 'Tuesday':
        $ord = "`id` DESC, FIELD(`classgroup`, 'A', 'B', 'C', 'D')";
        $lim = 4;
        break;
    case 'Wednesday':
        $ord = "`id` DESC, FIELD(`classgroup`, 'F', 'C', 'E')";
        $lim = 3;
        break;
    case 'Thursday':
        $ord = "`id` DESC, FIELD(`classgroup`, 'F', 'E', 'D')";
        $lim = 3;
        break;
    case 'Friday':
        $ord = "`id` DESC, FIELD(`classgroup`, 'F', 'B', 'A')";
        $lim = 3;
        break;
}


$q = "SELECT `id`, `classgroup`, `body`, `pointcles`, `arevoir`, DATE_FORMAT(created, '%a, %b %D') as `cr` FROM `homework` ORDER BY $ord LIMIT $lim";

 

Link to comment
https://forums.phpfreaks.com/topic/291621-select-according-to-particular-order/
Share on other sites

  • Solution

As you need the "most recent" then there should be a date field in there instead of relying on id sequences. I would also ad a "daygroup" table to define the groups you require on each day and the sequence you want them in.

+------------+
|  daygroup  |
+------------+
| id         |
| day        |
| classgroup |
| seq        |
+------------+

eg:

+----+------+------------+------+
| id | day  | classgroup | seq  |
+----+------+------------+------+
|  1 |    2 | A          |    1 |
|  2 |    2 | B          |    2 |
|  3 |    2 | C          |    3 |
|  4 |    2 | D          |    4 |
|  5 |    2 | E          |    5 |
|  6 |    3 | F          |    1 |
|  7 |    3 | E          |    2 |
|  8 |    3 | D          |    3 |
| etc                           |
+----+------+------------+------+

Then use a subquery to determine the latest date for each classgroup. So now you have

SELECT 
    h.date
  , h.classgroup
FROM homework h
    INNER JOIN daygroup dg USING (classgroup)
    INNER JOIN (
        SELECT classgroup, MAX(date) as date
        FROM homework
        GROUP BY classgroup
        ) as latest USING (classgroup, date)
WHERE dg.day = DAYOFWEEK(CURDATE())
ORDER BY dg.seq
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.