从一个表中选择,从另一个 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 链接的表中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)