Skip to main content
added 1 character in body
Source Link
ivanm
  • 138
  • 1
  • 8

Please try the following query

SELECT *
FROM (SELECT postNumber, 
             user, 
             text,
             date,
             @current_portal := portal,
             @portal_rank := IF(@current_portal = portal and @portal_rank = portal, @portal_rank + 1, 1) AS portal_rank,
      FROM guestbook
      ORDER BY portal, date DESC) ranked
WHERE portal_rank = 1)

Rank the portal, using the portal_rank variable, and order by portal and date desc. In this way you are selecting only the latest record for every portal

I think this query is easier to understand and there is no need to do multiple joins on a single table when you can use a variable instead.

Additionally switching the condition portal_rank = 1 with portal_rank lets say =<= 3 you can get 3 latest records per portal with minimum query changes.

Hope this helps.

Please try the following query

SELECT *
FROM (SELECT postNumber, 
             user, 
             text,
             date,
             @current_portal := portal,
             @portal_rank := IF(@current_portal = portal and @portal_rank = portal, @portal_rank + 1, 1) AS portal_rank,
      FROM guestbook
      ORDER BY portal, date DESC) ranked
WHERE portal_rank = 1)

Rank the portal, using the portal_rank variable, and order by portal and date desc. In this way you are selecting only the latest record for every portal

I think this query is easier to understand and there is no need to do multiple joins on a single table when you can use a variable instead.

Additionally switching the condition portal_rank = 1 with portal_rank lets say = 3 you can get 3 latest records per portal with minimum query changes.

Hope this helps.

Please try the following query

SELECT *
FROM (SELECT postNumber, 
             user, 
             text,
             date,
             @current_portal := portal,
             @portal_rank := IF(@current_portal = portal and @portal_rank = portal, @portal_rank + 1, 1) AS portal_rank,
      FROM guestbook
      ORDER BY portal, date DESC) ranked
WHERE portal_rank = 1)

Rank the portal, using the portal_rank variable, and order by portal and date desc. In this way you are selecting only the latest record for every portal

I think this query is easier to understand and there is no need to do multiple joins on a single table when you can use a variable instead.

Additionally switching the condition portal_rank = 1 with portal_rank lets say <= 3 you can get 3 latest records per portal with minimum query changes.

Hope this helps.

Source Link
ivanm
  • 138
  • 1
  • 8

Please try the following query

SELECT *
FROM (SELECT postNumber, 
             user, 
             text,
             date,
             @current_portal := portal,
             @portal_rank := IF(@current_portal = portal and @portal_rank = portal, @portal_rank + 1, 1) AS portal_rank,
      FROM guestbook
      ORDER BY portal, date DESC) ranked
WHERE portal_rank = 1)

Rank the portal, using the portal_rank variable, and order by portal and date desc. In this way you are selecting only the latest record for every portal

I think this query is easier to understand and there is no need to do multiple joins on a single table when you can use a variable instead.

Additionally switching the condition portal_rank = 1 with portal_rank lets say = 3 you can get 3 latest records per portal with minimum query changes.

Hope this helps.