左连接MYSQL返回false

时间:2014-05-05 21:18:24

标签: php mysql left-join

我正在尝试执行MYSQL LEFT JOIN查询,但是我收到错误: "警告:mysqli_num_rows()要求参数1为mysqli_result,在"中给出布尔值。因为返回的查询是false。

代码是:

$query = "
SELECT a.page
     , SUM(b.views) views
     , ROUND(SUM(b.costs)/1.38,2) costs
     , ROUND(SUM(a.amount),2) REV
     , ROUND(SUM(a.amount) - SUM(b.costs)/1.38,2) PNL  
  FROM revenues a 
  LEFT 
  JOIN costs b 
    ON a.page = b.page
 WHERE a.date BETWEEN '$start_date' AND '$end_date' 
   AND a.country = '$country' 
   AND b.date BETWEEN '$start_date' AND '$end_date' 
 GROUP 
    BY a.page";

$result = mysqli_query($connection,$query);

if ($result != false) {
  $foundnum = mysqli_num_rows($result);
  if ($foundnum == 0) {
    echo 'No results found.';
  }
} else {
    echo 'returned false';
}

printQuery($result);

printQuery是我的一个功能,它创建了一个html表。

如果我对表格进行两次单独的查询,我会得到正确的表格。我的单独查询是:

$query = "SELECT page, round(sum(amount),2) as REV FROM revenues WHERE date between '$start_date' AND '$end_date' AND country = '$country' GROUP BY page" ;

$query2 = "SELECT page, sum(views) as views, round(sum(costs)/1.38,2) as costs FROM costs WHERE date between '$start_date' AND '$end_date' GROUP BY page";

有人可以指出我错误的地方。

表收入包括以下列: 日期:日期 amount:float(如果没有收入,则为NULL) country:varchar

表费用列数: 日期:日期 成本:浮动(总是> = 0) views:int

由于

1 个答案:

答案 0 :(得分:0)

以下是无用的:

else {
    echo 'returned false';
}

将其替换为:

else {
    echo "Query failed.\nMySQLi error: " . mysqli_error();
}

您至少可以检查返回值,但是至少出于开发目的,您需要使代码描述性失败。