Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 501043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:08:57+00:00 2026-05-13T06:08:57+00:00

I am writing the following sub query for some project specific purpose: SELECT count(*)

  • 0

I am writing the following sub query for some project specific purpose:

SELECT count(*) from table1 
WHERE userB='$p' AND userA IN 
  (SELECT userB FROM table1 
   WHERE userA='$row[username]')

I was curious if this was the best practice when doing it in PHP or should I resort to the conventional way of first getting the subquery result and then counting the records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:08:58+00:00Added an answer on May 13, 2026 at 6:08 am

    I was curious if this was the best practice when doing it in PHP or should I resort to the conventional way of first getting the subquery result and then counting the records?

    Leaving the SQL query in PHP or stored procedure holy war aside, less trips to the database is the best practice. There’s time to the database & back that can never be recouped, and separating the queries runs the risk of data changing in between the queries.

    Can the query itself be optimized? In this example, potentially yes:

    SELECT COUNT(*) 
      FROM TABLE t
      JOIN TABLE t2 ON t2.userB = t.userA
                   AND t2.userA = '$row[username]'
     WHERE t.userB = '$p' 
    

    If you really want to be sure about query performance, you’ll have to get familiar with generating an explain plan & interpreting the output to tune the query.

    What’s an Explain Plan?

    The MySQL explain plan shows how the MySQL query optimizer has decided to run a SELECT statement in order to best access the data that’s been requested.

    How Do I Generate an Explain Plan?

    In MySQL, you just have to add the keyword explain to the SELECT query, before the SELECT keyword. IE:

    EXPLAIN SELECT COUNT(*)
              FROM TABLE t
              JOIN TABLE t2 ON t2.userB = t.userA
                           AND t2.userA = '$row[username]'
             WHERE t.userB = '$p'
    

    Probably should have mentioned this earlier, but you don’t want to run this from within PHP because it won’t return what you queried for. Use whatever SQL IDE, like PHPMyAdmin/etc.

    I’ve Gots My Explain Plan, But What Does it Mean?!

    The MySQL EXPLAIN documentation is a good place to read up on each column that is returned, & what the column represents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've encountered the following issue. I like to use the following writing: SELECT Id,
I am writing a GAE application and am having some difficulty with the following
I'm having some trouble with a generic method I'm writing. It has the following
Can anyone tell me if writing a query in the following tsql syntax is
Would there a more elegant way of writing the following syntax? Thread t0 =
Consider the following two ways of writing a loop in Java to see if
I'm writing an app that contains the following tables: (1) employee_type, (2) employee and
I'm writing a small webapp in Grails and I have the following question regarding
When writing GUIs, I've frequently come over the following problem: Assume you have a
I'm learning C++ and writing little programs as I go along. The following is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.