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 8782503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:34:34+00:00 2026-06-13T20:34:34+00:00

I’m trying to INNER JOIN a single row from a table. The query returns

  • 0

I’m trying to INNER JOIN a single row from a table. The query returns rows until I add the LIMIT 1 line of code and then it returns nothing and it doesn’t make any sense to me. Here is my code.

SELECT DISTINCT pd.pid, fv.flow_bmi, fv.flow_date
FROM patient_data pd
INNER JOIN ( ... )
INNER JOIN (
  SELECT pid, flow_bmi, flow_date
  FROM flow_vitals
  ORDER BY flow_date DESC
) fv ON fv.pid = pd.pid

#Results
-------------------------------
 pid  | flow_bmi  | flow_date
-------------------------------
 6073 | 31.90     | 2011-11-06
 6073 | 33.10     | 2011-11-05 <-- I don't want this row returned
 6072 | 32.10     | 2011-08-01

And now when I add LIMIT 1

SELECT DISTINCT pd.pid, fv.flow_bmi, fv.flow_date
FROM patient_data pd
INNER JOIN ( ... )
INNER JOIN (
  SELECT pid, flow_bmi, flow_date
  FROM flow_vitals
  ORDER BY flow_date DESC
  LIMIT 1
) fv ON fv.pid = pd.pid

#Expected Outcome
-------------------------------
 pid  | flow_bmi  | flow_date
-------------------------------
 6073 | 31.90     | 2011-11-06
 6072 | 32.10     | 2011-08-01

#Actual Outcome
-------------------------------
 pid  | flow_bmi  | flow_date
-------------------------------
 No rows returned.

I don’t know why LIMIT breaks this, and what I really want is an INNER JOIN that returns only one row based on the latest date. It seems like there are a ton of questions on this same topic but they end up using MAX() or LIMIT which both have not worked for me.

  • 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-13T20:34:35+00:00Added an answer on June 13, 2026 at 8:34 pm

    You actually want one row per group, not one row total from the flow_vitals table. Putting a LIMIT 1 there returns just the top row from the entire table.

    I assume the row you want is the one with the maximum flow_date for that pid. Try:

    SELECT DISTINCT pd.pid, fv.flow_bmi, flow_date
    FROM patient_data pd
    INNER JOIN ( ... )
    INNER JOIN (
      SELECT pid, flow_bmi, flow_date
      FROM flow_vitals
    ) fv ON fv.pid = pd.pid
    WHERE flow_date = (SELECT MAX(ffvv.flow_date) FROM flow_vitals ffvv WHERE ffvv.pid = fv.pid)
    

    Or, as a join:

    SELECT DISTINCT pd.pid, fv.flow_bmi, flow_date
    FROM patient_data pd
    INNER JOIN ( ... )
    INNER JOIN (
      SELECT pid, flow_bmi, flow_date
      FROM flow_vitals
    ) fv ON fv.pid = pd.pid
    INNER JOIN (
      SELECT pid, MAX(flow_date) AS maxDate
      FROM flow_vitals
      GROUP BY pid
    ) fvMax ON fv.pid = fvMax.pid AND fv.flow_date = fvMax.maxDate
    

    I also wonder if DISTINCT is really necessary here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.