Is it possible to group a table based on the condition in MYSQL?



  • I have table 'request' and I need to somehow group the table where the flag 'is_applicant' = 1 if exiting rows with the same 'id_request' and 'the same id_user' you can see it better by example...

    enter image description re

    I need to get this... enter image description here

    Thank you for the answer or advice how to achieve this, I need it to display the request to a specific user in php. 🙂



  • SELECT  id_request,
            MAX(is_applicant) AS is_applicant
        GROUP BY id_request
        ORDER BY id_request DESC
    

    I left out id since it is probably irrelevant.

    I left out id_user and id_status; perhaps you want MAX(id_user)? The issue is, if there could be two users or two statuses, which one do you want?

    If you do need id, then see tag [greatest-n-per-group].




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2