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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:08:42+00:00 2026-05-25T13:08:42+00:00

My db tables look like that First table: Second table – lastsrvc (Last service

  • 0

My db tables look like that

First table:

enter image description here

Second table – lastsrvc (Last service info). id column -uniques auto-incremented id for every issue. ‘cid’ column – company’s id. It’s the link column between 2 tables: main and lastsrvc.

enter image description here

Now, what I want to do is, lets say, count all issues for company 1. My code sends before while 1 query and fetches all rows of main table. Then sends second query within while for every company’s row.

If in the future there will be 10 000 company’s it will load server I think. Is there any way to send 1 query instead of sending query for every company (within while)? Is it possible to use COUNT before while, instead of second query within while?

My code looks like that

    <?php
    $query=$db->query("SELECT * FROM main");
    while($row=$query->fetch_object())
    {?>
        <tr> 
        <td><?=$row->id;?></td>
        <td><?=$row->company;?></td>
        <td><?=$row->address;?></td>
        <td><?=$row->contact_name;?></td>
        <td><?=$row->contact_phone;?></td>
        <td><?php 
        if ($row->warr)
        {
            echo 'Var,'.$row->warr_year.' il';
        }
        else {
            echo 'Yoxdur';
            }
        ?></td>
        <td>
        <?php
        if ($query2=$db->query("SELECT * FROM lastsrvc WHERE cid='$row->id'"))
        echo $query2->num_rows;
        ?>
        </td>
        </tr>
<?php } ?>
  • 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-25T13:08:43+00:00Added an answer on May 25, 2026 at 1:08 pm

    Use this as your top-level (and only) query:

    SELECT main.id, company, address, contact_name, contact_phone, COUNT(cid) as cnt
    FROM main
    LEFT JOIN lastsrvc ON main.id = lastsrvc.cid
    GROUP BY main.id
    

    One single query with a join is almost always going to be much more efficient than running 1+n queries. You just echo out that ‘cnt’ field as usual:

    <td><?php echo $row->cnt ?></td>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 3 tables that look like this: (source: InsomniacGeek.com ) On the
Let's say I have two tables that look like this: TH TH TH TH
I have a table posts that could look like this: id | title |
On my pages I have a table with rows that typically look like this:
I have a couple of tables which look like this Table 1 user_id |
I'm storing linked lists of data in records that look like this: CREATE TABLE
I have a number of mappers that look like this: mapper(Photo,photo_table, properties = {
My two tables look like this: TABLE1 TABLE2 +--------------------+ +--------------------+ |field1|field2|field3| and |field2|field4|field5| +--------------------+
I have three tables: videos, videos_categories, and categories. The tables look like this: videos:
I have 2 tables which in simplified form look like this: Products( id: int,

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.