简体   繁体   中英

LEFT JOIN on Max Value

Suppose I have the following two tables:

STUDENT
studentid   lastname   firstname
1           Smith      John
2           Drew       Nancy

STUDENT_STORY
studentid   dateline   storyid   status
1           1328313600 10        2
1           1328313601 9         1
2           1328313602 14        2
2           1328313603 12        1

Now, I need an SQL query that would select each student along with the latest story for that student in the student story table.

I am trying this:

SELECT s.*, ss.*
FROM student AS s
LEFT JOIN (
    SELECT *
    FROM student_story
    WHERE student_story.studentid = s.studentid
    ORDER BY dateline DESC LIMIT 1
) AS ss ON (ss.studentid = s.studentid)

However, this query does not work. It complains about s.studentid being an unknown field in the where clause of the sub query.

Please suggest how I can achieve what I'm trying to do.

Thanks.

Try something like this:

SELECT
  s.*,
  ss.*
FROM
  student AS s
LEFT JOIN
  student_story AS ss
ON (ss.studentid = s.studentid)
WHERE ss.dateline = (
  SELECT
    MAX(dateline)
  FROM
    student_story AS ss2
  WHERE
    ss2.studentid = s.studentid
)
SELECT s.*, ss.*
FROM student AS s
LEFT JOIN (

SELECT * FROM student_story    
ORDER BY dateline DESC LIMIT 1
) 
AS ss ON (ss.studentid = s.studentid)
SELECT 
    s.sale_id,
    s.created_at,
    p.created_at,
    DATEDIFF(p.created_at, s.created_at) AS days
FROM
    pos_sales s
        LEFT JOIN
    pos_payments p ON p.sale_id = s.sale_id
        AND
    p.created_at = (SELECT 
            MAX(p2.created_at)
        FROM
            pos_payments p2
        WHERE
            p2.sale_id = p.sale_id)

This join should do it:

SELECT s.*, ss.*
FROM student_story AS ss
LEFT JOIN student AS s
    ON student_story.studentid = s.userid
GROUP BY ss.studentid

GROUP BY should take last row, so last story = last row, if it won't work try to:

GROUP BY ss.studentid, ss.dateline DESC

I'm not sure about it, please comment with result

You'll need to add an auto and unique id on each row on student_story table.

then you will distinguish between them. ( assume studentstory.new_id )

select s.*, ss.*
from student s
left join student_story ss on ss.studentid = s.userid
where ss.new_id = ( select ss.new_id from student s
    group by dateline
    order by dateline desc
    limit 1
)

Another approach is making a LEFT JOIN with a NOT EXISTS() condition. Depending on the scenario, this can give a bit more flexibility; however, it will also show duplicate results if there are two entries with the same dateline per student:

SELECT s.*, ss.*
FROM student AS s
LEFT JOIN student_story AS ss ON ss.studentid = s.studentid AND NOT EXISTS
    (SELECT * FROM student_story AS ss2
     WHERE ss2.studentid = ss.studentid AND ss2.dateline > ss.dateline)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM