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

select from one table, count from another where id#39;s linked(从一个表中选择,从另一个 id 链接的表中计数)

本文介绍了从一个表中选择,从另一个 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   
}

?>

推荐答案

尝试改变...

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

...到...

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

这篇关于从一个表中选择,从另一个 id 链接的表中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从一个表中选择,从另一个 id 链接的表中计数

基础教程推荐