MySQL subqueries

Posted by swamprunner7 on Stack Overflow See other posts from Stack Overflow or by swamprunner7
Published on 2010-05-07T12:52:48Z Indexed on 2010/05/07 21:48 UTC
Read the original article Hit count: 259

Filed under:
|
|

Can we do this query without subqueries?

SELECT login, post_n,

(SELECT SUM(vote) FROM votes WHERE votes.post_n=posts.post_n)AS votes, 
(SELECT COUNT(comments.post_n) FROM comments WHERE comments.post_n=posts.post_n)AS comments_count 

FROM users, posts 
WHERE posts.id=users.id AND (visibility=2 OR visibility=3) 
ORDER BY date DESC LIMIT 0, 15

tables:

Users: id, login

Posts: post_n, id, visibility

Votes: post_n, vote

id — it`s user id, Users the main table.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about subqueries