Witam smile.gif Mam takie zapytanie:
  1. SELECT `b`.`id` AS `id`, `b`.`type` AS `type`, `b`.`name` AS `name`, `b`.`description` AS `description`, `b`.`hasChild` AS `hasChild`, `b`.`topicsCount` AS `topicsCount`, `b`.`messagesCount` AS `messagesCount`, `b`.`lastMsgTopic` AS `lastMsgTopic`, `b`.`lastMsgUser` AS `lastMsgUser`, `g`.`id` AS `lastMsgUserGroup`, `b`.`lastMsgDate` AS `lastMsgDate`
  2. FROM `fb_boards` `b`
  3. INNER JOIN `fb_boards_relations` `r` ON `b`.`id` = `r`.`childId`
  4. LEFT JOIN `fb_users_in_groups` `ug` ON `ug`.`userId` = `b`.`lastMsgUser`
  5. LEFT JOIN `fb_groups` `g` ON `g`.`id` = `ug`.`groupId`
  6. WHERE `r`.`depth` = 1 AND `r`.`parentId` = 0 ORDER BY `b`.`position` ASC;


Najistotnejsze w tabeli fb_boards jest tuaj pole lastMsgUser, które zawiera ID użytkownika dla którego buduję relację.

Aktualny stan tabeli fb_users_in_groups:
Kod
+--------+---------+
| userId | groupId |
+--------+---------+
|      2 |       1 |
|      2 |       2 |
|      2 |       3 |
+--------+---------+


fb_groups:
Kod
+----+-----------+
| id | relevance |
+----+-----------+
|  1 |         2 |
|  2 |         3 |
|  3 |         1 |
+----+-----------+


Zapytanie pobiera listę forów wraz z informacją o autorze ostatniego posta. Problem tkwi w pobraniu identyfikatora najważniejszej grupy do której należy ten user. W obecnej postaci dubluje wpis forum 3 razy. Czyli otrzymuję 3 wpisy z różniącym się lastMsgUserGroup (kolejno 3, 2, 1) dla lastMsgUser=2. Niestety, nie wiem jak to przebudować żeby brało pod uwagę istotność grupy (kolumna relevance w fb_groups - miało by pobrać tą grupę, w której wartość jest największa), w wyniku czego otrzymałbym lastMsgUserGroup = 2 dla lastMsgUser=2.

Proszę o pomoc smile.gif
Pozdrawiam