从一个表中选择,从另一个 id 链接的表中计数

2021-12-17 00:00:00 join sql select mysql

这是我的代码:

$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   
}

?>

推荐答案

尝试改变...

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

...到...

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

相关文章