I was curious if this was the best practice when doing this in PHP, or should I resort to the usual way of getting the result of a subquery and then counting the records?
Exiting a SQL query in PHP or keeping a holy war stored procedure aside, fewer trips to the database is best practice. Time to the database and vice versa, which can never be recouped, and the separation of queries leads to the risk of data changes between queries.
? "":
SELECT COUNT(*)
FROM TABLE t
JOIN TABLE t2 ON t2.userB = t.userA
AND t2.userA = '$row[username]'
WHERE t.userB = '$p'
, .
?
MySQL , MySQL SELECT .
?
MySQL SELECT SELECT. IE:
EXPLAIN SELECT COUNT(*)
FROM TABLE t
JOIN TABLE t2 ON t2.userB = t.userA
AND t2.userA = '$row[username]'
WHERE t.userB = '$p'
, , PHP, , . SQL IDE, PHPMyAdmin/etc.
Gots My Explain Plan, ?!
MySQL EXPLAIN - , .