If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > can I solve this query by using self join?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-07, 13:01
djuritz djuritz is offline
Registered User
 
Join Date: Sep 2005
Posts: 67
can I solve this query by using self join?

Hi, I've this table in MySQL.
It's from a website where you can post secrets and users can post comments to those secrets.
Columns are:
ID, post_date, content, comment_for

If post is new thread, comment_for will be null.
If post an answer to an existing secret, comment_for will be the ID of the original post.
ie:
Original post: ID = 100, DATE=2007-04-26, COMMENT_FOR=NULL, content=xxxxx
Reply: ID=999, DATE=2007-04-27 COMMENT_FOR=100, content=xxxxx

The point is that I need to know the most popular secrets (posts), and for doing it, I need to known the secrets with more answers.

How can I do that?
I known I have to use GROUP BY, but should I use a self referenced join or something like that?

Thanks in advance.
Diego.-
Reply With Quote
  #2 (permalink)  
Old 04-27-07, 13:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select t1.id
     , t1.post_date
     , t2.comments
  from secrets as t1
inner
  join (
       select id
            , count(comment_for) as comments
         from secrets
       group
           by id
       ) as t2
    on t2.id = t1.id
order
    by t2.comments desc limit 10
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-27-07, 13:45
djuritz djuritz is offline
Registered User
 
Join Date: Sep 2005
Posts: 67
Dear r937.

Thanks for your reply.
But I'm afraid MySQL 4.0 doesn't support on-line views as Oracle does.
Is there any other way?

Thanks!

Quote:
Originally Posted by r937
Code:
select t1.id
     , t1.post_date
     , t2.comments
  from secrets as t1
inner
  join (
       select id
            , count(comment_for) as comments
         from secrets
       group
           by id
       ) as t2
    on t2.id = t1.id
order
    by t2.comments desc limit 10
Reply With Quote
  #4 (permalink)  
Old 04-27-07, 17:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sorry, my answer was made on the assumption that the question was being asked by someone interested in Database Concepts & Design, not someone with an extremely antiquated version of a database system that at one point was unable to support the more complex but fully standard sql constructions

you should have posted in the mysql forum, and you should have mentioned your ancient version

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-27-07, 18:43
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Hint: Upgrade...
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On