Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:40:42+00:00 2026-06-04T00:40:42+00:00

I have four related tables and, using the following query, combine them with 4

  • 0

I have four related tables and, using the following query, combine them with 4 LEFT JOIN and 2 MAX() aggregate functions:

SELECT SQL_CALC_FOUND_ROWS
         id,
         cognome,
         nome,
         sesso,
         pr_sedute_complessive,
         presa_in_carico_data,
         cf,
         cnome,
         tdr_cognome,
         tdr_nome,
         COUNT(tbl_trattamenti.trt_id),
         MAX(tbl_trattamenti.data),
         pr_id
FROM     tbl_aziente p1
  LEFT JOIN comuni
         ON comuni.cid = p1.nascita_luogo 
  LEFT JOIN tbl_cartellaclinica
         ON tbl_cartellaclinica.pz_fk_id = p1.id 
  LEFT JOIN tbl_progetto
         ON tbl_progetto.cc_id_fk = tbl_cartellaclinica.cc_id 
  LEFT JOIN tbl_fisioterapista
         ON tbl_fisioterapista.tdr_id = tbl_progetto.pr_fisioterapista_fk 
  LEFT JOIN tbl_trattamenti
         ON tbl_progetto.pr_id = tbl_trattamenti.pr_fk_id 
WHERE    idoneo = 'y'
     AND p1.tipo_assistenza = 4
GROUP BY p1.id
LIMIT 0, 10

This is all okay, except for one thing: I want the record from tbl_progetto which has the highest id rather than the first. I’ve tried to use MAX(primaryKey), but I then get only the key – not the record having that pKey.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:40:43+00:00Added an answer on June 4, 2026 at 12:40 am

    The basic principle is that you need to reference tbl_progetto twice – once in a subquery that identifies the id of the records of interest, and a second time to fetch the rest of that record.

    Assuming that there is a many-to-one relationship between tbl_cartellaclinica and tbl_progetto, the subquery would be:

    SELECT   tbl_cartellaclinica.pz_fk_id, MAX(tbl_progetto.pr_id) AS pr_id
    FROM     tbl_cartellaclinica
        JOIN tbl_progetto
          ON tbl_progetto.cc_id_fk = tbl_cartellaclinica.cc_id
    GROUP BY tbl_cartellaclinica.pz_fk_id
    

    And therefore your modified query would be:

    SELECT SQL_CALC_FOUND_ROWS
             id,
             cognome,
             nome,
             sesso,
             pr_sedute_complessive,
             presa_in_carico_data,
             cf,
             cnome,
             tdr_cognome,
             tdr_nome,
             COUNT(tbl_trattamenti.trt_id),
             MAX(tbl_trattamenti.data),
             t.pr_id
    FROM     tbl_aziente p1
      LEFT JOIN comuni
             ON comuni.cid = p1.nascita_luogo 
      LEFT JOIN tbl_cartellaclinica
             ON tbl_cartellaclinica.pz_fk_id = p1.id
      LEFT JOIN (
        SELECT   tbl_cartellaclinica.pz_fk_id, MAX(tbl_progetto.pr_id) AS pr_id
        FROM     tbl_cartellaclinica
            JOIN tbl_progetto
              ON tbl_progetto.cc_id_fk = tbl_cartellaclinica.cc_id
        GROUP BY tbl_cartellaclinica.pz_fk_id
      ) AS t ON t.pz_fk_id = tbl_cartellaclinica.pz_fk_id
      LEFT JOIN tbl_progetto
             ON tbl_progetto.cc_id_fk = tbl_cartellaclinica.cc_id
            AND tbl_progetto.pr_id = t.pr_id
      LEFT JOIN tbl_fisioterapista
             ON tbl_fisioterapista.tdr_id = tbl_progetto.pr_fisioterapista_fk 
      LEFT JOIN tbl_trattamenti
             ON tbl_progetto.pr_id = tbl_trattamenti.pr_fk_id 
    WHERE    idoneo = 'y'
         AND p1.tipo_assistenza = 4
    GROUP BY p1.id
    LIMIT    0, 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four tables. Three of them are tables with one key and they
I have four tables: Companies, Products, Customers and Sales. They are related as follows:
I'm using TabHost and I have four tabs. When I start the related activity
I have four models that are related to one another, the way I have
i have four tables user-question contains two columns: questionID, userID, the questions that the
I have customized uitableViewcell in which each cell show four data which are related
I have an OpenGL-related issue. Whenever I attempt to draw a simple polygon using
Acttualy this is my assignment.I have three-four file,related by student record.Every file have two-three
In Access 2007, I have two tables related such that the (surrogate) PK of
I have a select menu with about four options. The first option of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.