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 4233784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:15:52+00:00 2026-05-21T02:15:52+00:00

i have this three query i would like to minimize this to one. how

  • 0

i have this three query i would like to minimize this to one. how would i do it?

    $query  = "SELECT COUNT(*) FROM states WHERE id = 1";
    $query2 = "SELECT COUNT(*) FROM cities WHERE id = 5";
    $query3 = "SELECT COUNT(*) FROM areas WHERE id = 3";

also i would appreciate if someone points me to some great website or article that could help me practice and understand MySQL commands. thanks a lot.

EDIT : i would like the script to return the number of rows combined in one result(variable).

  • 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-21T02:15:53+00:00Added an answer on May 21, 2026 at 2:15 am

    Well, the only way (i know) to do this in one query is this:

    SELECT 
    (SELECT COUNT(*) FROM states WHERE id = 1) AS state_count,
    (SELECT COUNT(*) FROM cities WHERE id = 5) AS city_count,
    (SELECT COUNT(*) FROM areas WHERE id = 3) AS area_count
    

    This hardly can be called an “optimization”, beacuse there are also 3 table scans, but can’t avoid those, since you want to COUNT on three different tables.

    EDIT

    If you want a sum of three numbers, then this is the answer:

    SELECT 
    (
        (SELECT COUNT(*) FROM states WHERE id = 1) 
        + (SELECT COUNT(*) FROM cities WHERE id = 5) 
        + (SELECT COUNT(*) FROM areas WHERE id = 3)
    ) AS sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
I have markup like this: <p>one two three four</p> And I want to use
I have three tables that I would like to query: Streams, Entries, and FieldInstances.
We have three tables that we would like to query to find out the
I have three tables. This query will write down the right answer (x-lines for
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have three DIVs, something like this: <div id=header> ... </div> <div id=content> <div
I'm completely stumped on this one. I have three different lists that need to
I would like to do MySQL query like this: if((value1 != dbPrimaryValue1) OR (value2
I have three tables: user, friend_recommendation, and movie. I would like to get the

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.