加入查询不工作返回null结果

时间:2014-10-08 05:54:58

标签: php mysql sql

我想按日期和域计算记录数。 php查询在phpmyadmin中工作得很好,但在 php代码中它返回null。我希望像今天,明天一样按日期显示。加入表格和按日期和域名计算。

wp_domain_prefix TABLE enter image description here

wp_projectid_1表

enter image description here

PHP查询

  <?php
 $querys = "SELECT `wp_domain_prefix.domain`, count( `wp_projectid_1.record_id` )
 FROM `wp_domain_prefix`, `wp_projectid_1` WHERE `wp_domain_prefix.domain` = `wp_projectid_1.domain` AND 
    `wp_projectid_1.datess` BETWEEN '2014-10-07' AND '2014-10-08'";
    $record = mysql_query($querys);
    echo $record[count('wp_projectid_1.record_id')];
 ?>

3 个答案:

答案 0 :(得分:1)

将查询更改为,

$querys = "SELECT wp_domain_prefix.domain, count(wp_projectid_1.record_id) FROM wp_domain_prefix, wp_projectid_1 WHERE wp_domain_prefix.domain = wp_projectid_1.domain WHERE 
wp_projectid_1.datess BETWEEN '2014-10-07' AND '2014-10-08'";
$record = mysql_query($querys);
$row = mysql_fetch_array($record);
echo $row['record_id']; 

答案 1 :(得分:0)

您需要将GROUP BY用于聚合函数,例如COUNT()

$querys = 
    "SELECT wp_domain_prefix.domain, count( wp_projectid_1.record_id ) 
    FROM wp_domain_prefix, wp_projectid_1 
    WHERE wp_domain_prefix.domain = wp_projectid_1.domain
    AND wp_projectid_1.datess BETWEEN '2014-10-07' AND '2014-10-08'
    GROUP BY wp_domain_prefix.domain";

$record = mysql_query($querys);
$row = mysql_fetch_row($record);
print_r($row);

Read more about GROUP BY

答案 2 :(得分:0)

最好使用mysqli而不是mysql,因为不推荐使用mysql,支持很快就会结束。

mysql_fetch_row尝试这样的

之后,您无法从mysql_query获取数据
$querys = "SELECT `wp_domain_prefix.domain`, count( `wp_projectid_1.record_id` ) as totalcount FROM `wp_domain_prefix`, `wp_projectid_1` WHERE `wp_domain_prefix.domain` = `wp_projectid_1.domain` AND 
`wp_projectid_1.datess` BETWEEN '2014-10-07' AND '2014-10-08'";

$record = mysql_query($querys);
$row = mysql_fetch_row($record);
print_r($row);
echo $row['totalcount'];