// post · 179
For future reference
SELECT news.*, COUNT(comments.newsid) AS count FROM news
LEFT JOIN comments ON news.id=comments.newsid
GROUP BY news.id ORDER BY Id DESC
...don't mind me.
EDIT: Excellent down to 0.16 from 0.26 seconds. And down to 1 query from a shocking 162 on my little test. Very very good.
LEFT JOIN comments ON news.id=comments.newsid
GROUP BY news.id ORDER BY Id DESC
...don't mind me.
EDIT: Excellent down to 0.16 from 0.26 seconds. And down to 1 query from a shocking 162 on my little test. Very very good.
// leave a comment
HTML allowed: <a href="" title="" rel=""></a> <b></b> <blockquote cite=""></blockquote> <em></em> <i></i> <strike></strike> <strong></strong> <li></li> <ol></ol> <ul></ul>
ie: <b>bold</b>
Your comment may need to be reviewed before it is published.
// comments
Actually, I really love the power of LEFT JOIN.
Oooh, I love SQL.
Now I can get the number of comments at the same time as getting the news posts themselves. So two different tables at one time.
LEFT JOIN solves a lot of problems of multiple queries, but it acts really weird sometimes.
Also, it depends on how the tables are structured. If they are structured sloppily - like they are in my case - LEFT JOIN won't solve any problems.
The first step is solid relational database theory.