Pregunta

AGENT DB TABLE

agent_id  agent_name company_name
--------  ----------  -----------
1         AAA           XXX
2         BBB           YYY
3         CCC           ZZZ
4         DDD           XYZ

DRIVER DB TABLE

agent_id  driver_id   driver_name
--------  ----------  -----------
2         1           EEE
2         2           FFF
2         3           GGG
1         4           HHH
3         5           III
3         6           JJJ

I WANT TO SHOW THE RESULT LIKE THIS

AGENT DETAILS    DRIVER DETAILS
------------     --------------  
 1, AAA, XXX         1 Driver           
 2, BBB, YYY         3 Drivers           
 3, CCC, ZZZ         2 Drivers
 4, DDD, XYZ         0 Driver

I HAVE TRIED THIS BUT I GOT THE RESULT LIKE THIS

AGENT DETAILS    DRIVER DETAILS
------------     --------------  
 1, AAA, XXX         1 Drivers           
 1, AAA, XXX         3 Drivers           
 1, AAA, XXX         2 Drivers
 1, AAA, XXX            -

I have attached sample image and code below..

enter image description here

I hope someone can understand my problem. Here I have to show the agent and driver details. Agent table have agent_id, agent_name, company_name. I want to fetch all records from agent table. Then I want to count the number of drivers based on agent_id.

In my driver db table I have agent_id, driver_id, driver_name. I want to fetch the number of drivers from driver table based on agent_id. This is one works fine. But, My problem is I have multiple while loops so it won't fetch properly. See my attached image. You can understand easily. In Agent Details it always fetch the same rows in all rows.

I'm sure I made a mistake in while loop. But, I don't know how do I get the proper result..

<?php
$sql="select * from ".TBL_AGENT."";
$result=mysql_query($sql,$CN);



while($row=mysql_fetch_array($result))
{
    $agentid = $row['agent_id'];
    $agentname = $row['agent_name'];
    $companyname = $row['company_name'];
    $email = $row['email'];

    $sql_query = mysql_query("SELECT agent_id, COUNT(driver_id) AS ".TBL_DRIVER." FROM ".TBL_DRIVER."  GROUP BY $agentid");         while($rows = mysql_fetch_array($sql_query, MYSQL_ASSOC))
    { $noofdrivers = $rows['ta_drivers']. " DRIVERS"; echo "<br/>"; 
?>
    <tr>
         <td bgcolor="#FFFFFF" style="height: 70px; width: 300px; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc;">
            <b>Agent Name : </b><?=$agentname?><br /><br />
            <b>Business Name : </b><?=$companyname?><br /><br />
            <b>E-mail Id : </b><?=$email?>
         </td>
         <td bgcolor="#FFFFFF" style="height: 70px; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc;">
        <?php 
           echo $noofdrivers; ?>
         </td>
         <td bgcolor="#FFFFFF" style="height: 70px; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc;"></td>
         <td bgcolor="#FFFFFF" style="height: 70px; border-bottom: 1px solid #ccc;"></td>   
    </tr>
<?php }  } ?>
¿Fue útil?

Solución

As from comments here is the answer ,you can join your tables as and loop over them in php

SELECT a.*,
COUNT(d.driver_id) `drivers_count`
FROM AGENT a
LEFT JOIN DRIVER d USING(agent_id)
GROUP BY a.agent_id

A fiddle Demo

Otros consejos

  SELECT a.agent_id, 
         a.agent_name,
         a.company_name,
         count(d.*) as count_of_drivers
    FROM agent a, driver d
   WHERE a.id_agent = d.id_agent
GROUP BY a.agent_id,
         a.agent_name,
         a.company_name

Use one query with join (and don't use old-style joins with coma)

 SELECT agent.agent_id, agent.agent_name, agent.company_name, count(driver_id) as cnt
    FROM agent left join driver on agent.agent_id = driver.agent_id
    group by agent.agent_id

or you can use this

 SELECT agent_id, agent_name, company_name, 
    (Select count(*) FROM driver d WHERE d.agent_id = m.agent_id) AS cnt FROM agent m

It is understandable, but can be slower on big amounts of data

SQLfiddle:

http://sqlfiddle.com/#!2/52e28/9/0

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top