'order by not working on group by same samity_id and staff_id
I have 5 tables. I want latest staff name only. In order to better understand, I mentioned all the information in the table. If anyone has a problem with understanding or if you have any information, please comment.
Please read the whole and follow the information given at the bottom of the page.
Table 1: samities
+-----------+------------------------+-----------+--------+---------+---------+------------+---------+--------+---------------------+
| samity_id | name | office_id | day_id | code | gend_id | open_date | user_id | status | create_at |
+-----------+------------------------+-----------+--------+---------+---------+------------+---------+--------+---------------------+
| 1 | Torofmeru Park MS | 1 | 3 | 001-001 | 2 | 2022-03-14 | 2 | 1 | 2022-03-14 16:36:13 |
| 2 | Gabtoli Master Para MS | 1 | 6 | 001-002 | 2 | 2022-03-16 | 2 | 1 | 2022-03-16 14:43:40 |
+-----------+------------------------+-----------+--------+---------+---------+------------+---------+--------+---------------------+
Table 2: samity_assignment
+----------+----------+-----------+-----------+---------+--------+-----------+
| sasgn_id | staff_id | samity_id | office_id | user_id | status | create_at |
+----------+----------+-----------+-----------+---------+--------+-----------+
| 1 | 1 | 1 | 1 | 2 | 1 | NULL |
| 2 | 2 | 1 | 1 | 2 | 1 | NULL |
+----------+----------+-----------+-----------+---------+--------+-----------+
Table 3: employees
+----------+-------------+------+-----------+--------+
| staff_id | name | code | office_id | status |
+----------+-------------+------+-----------+--------+
| 1 | Staff Mr. A | 1 | 1 | 1 |
| 2 | Staff Mr. B | 2 | 2 | 1 |
+----------+-------------+------+-----------+--------+
Table 4: samity_days
+--------+-------------+--------------+--------+
| day_id | define_long | define_short | status |
+--------+-------------+--------------+--------+
| 1 | Saturday | Sat | 1 |
| 2 | Sunday | Sun | 1 |
| 3 | Monday | Mon | 1 |
| 4 | Tuesday | Tue | 1 |
| 5 | Wednesday | Wed | 1 |
| 6 | Thursday | Thu | 1 |
| 7 | Friday | Fri | 2 |
+--------+-------------+--------------+--------+
Table 5: genders
+---------+-------------+--------------+--------+
| gend_id | define_long | define_short | status |
+---------+-------------+--------------+--------+
| 1 | Male | M | 1 |
| 2 | Female | F | 1 |
| 3 | Others | O | 1 |
+---------+-------------+--------------+--------+
My Query:
SELECT
`sam`.`samity_id` `id`,
`sam`.`name`,
`sam`.`code`,
`emp`.`name` `staff_name`,
`day`.`define_short` `day_name`,
`gen`.`define_long` `gender_name`,
`sam`.`open_date` `opening`,
`sam`.`status`,
( SELECT COUNT( saa.sasgn_id ) FROM samity_assignment saa WHERE sam.samity_id = saa.samity_id ) member_total,
`saa`.`sasgn_id` `samity_assingment_id`
FROM
`samities` `sam`
LEFT JOIN `samity_assignment` `saa` ON `saa`.`samity_id` = `sam`.`samity_id`
LEFT JOIN `employees` `emp` ON `saa`.`staff_id` = `emp`.`staff_id`
LEFT JOIN `samity_days` `day` ON `sam`.`day_id` = `day`.`day_id`
LEFT JOIN `genders` `gen` ON `sam`.`gend_id` = `gen`.`gend_id`
GROUP BY
`saa`.`samity_id`
ORDER BY
`sam`.`name` ASC,
`saa`.`sasgn_id` DESC
LIMIT 25
Current Result:
Array
(
[0] => stdClass Object
(
[id] => 2
[name] => Gabtoli Master Para MS
[code] => 001-002
[staff_name] =>
[day_name] => Thu
[gender_name] => Female
[opening] => 2022-03-16
[status] => 1
[member_total] => 0
[samity_assingment_id] =>
)
[1] => stdClass Object
(
[id] => 1
[name] => Torofmeru Park MS
[code] => 001-001
[staff_name] => Staff Mr. A
[day_name] => Mon
[gender_name] => Female
[opening] => 2022-03-14
[status] => 1
[member_total] => 2
[samity_assingment_id] => 1
)
)
I want:
Array
(
[0] => stdClass Object
(
[id] => 2
[name] => Gabtoli Master Para MS
[code] => 001-002
[staff_name] =>
[day_name] => Thu
[gender_name] => Female
[opening] => 2022-03-16
[status] => 1
[member_total] => 0
[samity_assingment_id] =>
)
[1] => stdClass Object
(
[id] => 1
[name] => Torofmeru Park MS
[code] => 001-001
[staff_name] => Staff Mr. B
[day_name] => Mon
[gender_name] => Female
[opening] => 2022-03-14
[status] => 1
[member_total] => 2
[samity_assingment_id] => 1
)
)
There are two staff_id in the same samity_id. I just want to have the latest staff_id. I hope someone will help me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
