从一个表中选择,从另一个表中进行计数,其中id链接到该表 [英] select from one table, count from another where id's linked

查看:89
本文介绍了从一个表中选择,从另一个表中进行计数,其中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)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆