一尘不染

从一个表中选择,从另一个表中进行计数

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");

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

我得到的继承人错误:

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

这是我的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   
}

?>

阅读 448

收藏
2020-05-17

共1个答案

一尘不染

尝试改变…的喜欢

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

…至…

(select count(*) from bookings where b.id_customer = c.id)
2020-05-17