Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

sql - Resulting data on last thread_date

As I fixed the previous question thanks to the answer, I am sticking now in the part to retrieve a thread based on the last thread_date.

The code itself seems to be working fine, but it is only printing one result out instead of others.

The thread has a threads.cat_id which is linked to thesubsubcategory.extra_cat_id.

SELECT 
parent.subcat_id, 
parent.subcat_name, 
child.subsubcat_name, 
child.subcat_id, 
child.cat_id, 
kid.thread_name, 
kid.cat_id, 
kid.thread_date 
FROM 
subcategories parent  
JOIN subsubcategories child 
ON child.cat_id = parent.cat_id
 JOIN threads kid ON child.extra_cat_id = kid.cat_id 
WHERE thread_date = (SELECT MAX(thread_date) FROM threads)

What I am expecting is this:

 Category
    Subcategory       Latest thread
    Subcategory       Latest thread

What I am getting is this:

Category
  Subcategory       Latest thread  

SQL fiddle: http://sqlfiddle.com/#!9/52e27/2

Any solutions to it?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If my guess is right, you want to edit the where clause to show more based on last date as your threaddate is a datetime, you have to convert it to a date format before comparing it.

    SELECT parent.subcat_id,
        parent.subcat_name,
        child.subsubcat_name,
        child.subcat_id,
        child.cat_id,
        kid.thread_name,
        kid.cat_id,
        kid.thread_date
    FROM subcategories parent
    INNER JOIN subsubcategories child
        ON child.cat_id = parent.cat_id
    INNER JOIN threads kid
        ON child.extra_cat_id = kid.cat_id
WHERE convert(date,thread_date,108) = (
        SELECT MAX(convert(date,thread_date,108))
        FROM threads
        )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...