从一个表中选择,从另一个表中进行计数,其中id链接到该表

问题描述:

这是我的代码:

$sql = mysql_query("select c.name, c.address, c.postcode, c.dob, c.mobile, c.email, 
                    count(select * from bookings where b.id_customer = c.id) as purchased, count(select * from bookings where b.the_date > $now) as remaining, 
                    from customers as c, bookings as b 
                    where b.id_customer = c.id
                    order by c.name asc");

您可以看到我要执行的操作,但是我不确定如何正确地编写此查询.

you can see what i am trying to do, but im not sure how to write this query properly.

这是我得到的错误:

警告:mysql_fetch_assoc():已提供 参数不是有效的MySQL结果 资源

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

这是我的mysql_fetch_assoc:

heres my mysql_fetch_assoc:

<?php

while ($row = mysql_fetch_assoc($sql))
{
    ?>

    <tr>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['mobile']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><?php echo $row['purchased']; ?></td>
    <td><?php echo $row['remaining']; ?></td>
    </tr>

    <?php   
}

?>

尝试更改...的喜欢程度

Try changing the likes of...

count(select * from bookings where b.id_customer = c.id)

...到...

(select count(*) from bookings where b.id_customer = c.id)