mysql greatest n per group: latest message content and time for each conversationLimit WHERE to MAX() &...

Method to test if a number is a perfect power?

How does Loki do this?

Would this custom Sorcerer variant that can only learn any verbal-component-only spell be unbalanced?

How do scammers retract money, while you can’t?

Closest Prime Number

Lay out the Carpet

Why are there no referendums in the US?

Is HostGator storing my password in plaintext?

Do sorcerers' Subtle Spells require a skill check to be unseen?

Crossing the line between justified force and brutality

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

How to pronounce the slash sign

What is the opposite of 'gravitas'?

How do I find the solutions of the following equation?

Is there a korbon needed for conversion?

How did Doctor Strange see the winning outcome in Avengers: Infinity War?

Two monoidal structures and copowering

How long to clear the 'suck zone' of a turbofan after start is initiated?

Do the temporary hit points from the Battlerager barbarian's Reckless Abandon stack if I make multiple attacks on my turn?

Opposite of a diet

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

How to run a prison with the smallest amount of guards?

Is expanding the research of a group into machine learning as a PhD student risky?

What is the best translation for "slot" in the context of multiplayer video games?



mysql greatest n per group: latest message content and time for each conversation


Limit WHERE to MAX() & COUNT()mysql select latest per group using 2 different tables along with self joinGreatest N per Group in SQL ServerAlternative to removing ONLY_FULL_GROUP_BYIncompatible with sql_mode:only_full_group_byTroubles with: sql_mode=only_full_group_byProblem with only_full_group_byGetting the latest value using unique ID and user ID in MySQLGet a column from the latest row by another columnHow to optimize UPDATE with a nested SELECT subquery?













0















I have two tables, one for topics, and one for messages.
In terms of relationship, messages are posted within a topic, so each topic has many messages.



I am trying to make a query that would:
- list all the topics
- get the count of messages for each topic
- get the latest message posted for each topic (time and content)



I basically tried to do a query similar to:



SELECT
topic.*,
COUNT(msg.id),
MAX(msg.time),
msg.content

FROM topics

LEFT JOIN (
SELECT
msg.id,
msg.time,
msg.content,
msg.topic_id
FROM messages AS msg
ORDER BY time DESC
) AS msg
ON msg.topic_id=topic.id

GROUP BY topic.id


However, this results in an error Expression #... (closed) of SELECT list is not in GROUP BY clause and contains nonaggregated column 'msg.time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



I understand that I could just toggle the only_full_group_by setting, but my understanding is that I then have no guarantee that the msg.content will return me the latest message as expected.



I found many greatest n per group questions online, but most seem to only deal with a single table with the same table being queried in the query and subquery.



Is there any reliable way to achieve this with MySQL?









share







New contributor




Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I have two tables, one for topics, and one for messages.
    In terms of relationship, messages are posted within a topic, so each topic has many messages.



    I am trying to make a query that would:
    - list all the topics
    - get the count of messages for each topic
    - get the latest message posted for each topic (time and content)



    I basically tried to do a query similar to:



    SELECT
    topic.*,
    COUNT(msg.id),
    MAX(msg.time),
    msg.content

    FROM topics

    LEFT JOIN (
    SELECT
    msg.id,
    msg.time,
    msg.content,
    msg.topic_id
    FROM messages AS msg
    ORDER BY time DESC
    ) AS msg
    ON msg.topic_id=topic.id

    GROUP BY topic.id


    However, this results in an error Expression #... (closed) of SELECT list is not in GROUP BY clause and contains nonaggregated column 'msg.time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



    I understand that I could just toggle the only_full_group_by setting, but my understanding is that I then have no guarantee that the msg.content will return me the latest message as expected.



    I found many greatest n per group questions online, but most seem to only deal with a single table with the same table being queried in the query and subquery.



    Is there any reliable way to achieve this with MySQL?









    share







    New contributor




    Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I have two tables, one for topics, and one for messages.
      In terms of relationship, messages are posted within a topic, so each topic has many messages.



      I am trying to make a query that would:
      - list all the topics
      - get the count of messages for each topic
      - get the latest message posted for each topic (time and content)



      I basically tried to do a query similar to:



      SELECT
      topic.*,
      COUNT(msg.id),
      MAX(msg.time),
      msg.content

      FROM topics

      LEFT JOIN (
      SELECT
      msg.id,
      msg.time,
      msg.content,
      msg.topic_id
      FROM messages AS msg
      ORDER BY time DESC
      ) AS msg
      ON msg.topic_id=topic.id

      GROUP BY topic.id


      However, this results in an error Expression #... (closed) of SELECT list is not in GROUP BY clause and contains nonaggregated column 'msg.time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



      I understand that I could just toggle the only_full_group_by setting, but my understanding is that I then have no guarantee that the msg.content will return me the latest message as expected.



      I found many greatest n per group questions online, but most seem to only deal with a single table with the same table being queried in the query and subquery.



      Is there any reliable way to achieve this with MySQL?









      share







      New contributor




      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have two tables, one for topics, and one for messages.
      In terms of relationship, messages are posted within a topic, so each topic has many messages.



      I am trying to make a query that would:
      - list all the topics
      - get the count of messages for each topic
      - get the latest message posted for each topic (time and content)



      I basically tried to do a query similar to:



      SELECT
      topic.*,
      COUNT(msg.id),
      MAX(msg.time),
      msg.content

      FROM topics

      LEFT JOIN (
      SELECT
      msg.id,
      msg.time,
      msg.content,
      msg.topic_id
      FROM messages AS msg
      ORDER BY time DESC
      ) AS msg
      ON msg.topic_id=topic.id

      GROUP BY topic.id


      However, this results in an error Expression #... (closed) of SELECT list is not in GROUP BY clause and contains nonaggregated column 'msg.time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



      I understand that I could just toggle the only_full_group_by setting, but my understanding is that I then have no guarantee that the msg.content will return me the latest message as expected.



      I found many greatest n per group questions online, but most seem to only deal with a single table with the same table being queried in the query and subquery.



      Is there any reliable way to achieve this with MySQL?







      mysql greatest-n-per-group





      share







      New contributor




      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 min ago









      Simon NinonSimon Ninon

      101




      101




      New contributor




      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Simon Ninon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Simon Ninon is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233319%2fmysql-greatest-n-per-group-latest-message-content-and-time-for-each-conversatio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Simon Ninon is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Simon Ninon is a new contributor. Be nice, and check out our Code of Conduct.













          Simon Ninon is a new contributor. Be nice, and check out our Code of Conduct.












          Simon Ninon is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233319%2fmysql-greatest-n-per-group-latest-message-content-and-time-for-each-conversatio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...