MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 15th, 2006, 11:18 PM
haugstrup haugstrup is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 haugstrup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 31 sec
Reputation Power: 0
Cool Joining 2 tables with a count and subquery?

I have this problem that i just cant seem to solve :

We are making a very simple PHP / MySql forum containing only 2 tables.

1. forum_headers.
2. forum_notes.

When the user first sees the forum i would like to show:

forum_headers.subject

AND

A count of the forum notes that have have the forum_headers.id in the forum_notes.headerId field

AND

The newest forum_notes.created (timestamp) again with the forum_headers.id in the forum_notes.headerId field.

So that the output is something like this :

Subject, Record count, Latest record for the subject.

I dont know if it is possible, i have searched and searched..

Thank you in advance!

Jeppe Haugstrup

Reply With Quote
  #2  
Old February 17th, 2006, 12:12 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
Code:
SELECT
 fh.subject
,COUNT(fn.record) AS cnt
,(SELECT
    record
  FROM
    forum_notes
  WHERE
    headerID = fh.id
  ORDER BY
    forum_notes.created DESC
  LIMIT 1) AS latest
FROM
 forum_headers AS fh
INNER JOIN
 forum_notes AS fn
GROUP BY
 fh.subject
,latest
Comments on this post
Icon agrees: Haven't checked but this looks fine to me..
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Reply With Quote
  #3  
Old February 20th, 2006, 03:46 AM
haugstrup haugstrup is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 haugstrup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 31 sec
Reputation Power: 0
Thank you very much!..

Jeppe Haugstrup

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Joining 2 tables with a count and subquery?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway