2009-09-30 7 views
0

table: postID | userid | post | replytoMYSQL MyISAM Comment inscrire 2 instruction select + select count

post sql

SELECT * FROM table WHERE postID = 12

total des réponses sql

SELECT COUNT (*) AS totale FROM table WHERE replyto = 12

le résultat attendu est la "table post" + nombre de réponses au po st. Le champ replyto est le postid cible. somehing comme:

postID | | userid | post REPLYTO | totalreplies

Est-il possible de joindre ce 2 requêtes?

Merci!

Répondre

2

Vous pouvez l'utiliser comme un (5.x> seulement) sous-requête:

SELECT 
    postid, 
    userid, 
    post, 
    replyto, 
    (SELECT 
     COUNT(*) AS total 
    FROM table 
    WHERE replyto=12) AS totalreplies 
FROM table 
WHERE postid=12 

Je pense aussi se joindre à travailler pourrait, mais maintenant je ne vois pas comment.

0
SELECT 
    postid, userid, post, replyto, det.nb 
FROM 
    table, 
    (SELECT COUNT(*) AS nb FROM table) det