内部联接不从两个表中检索数据

时间:2016-10-13 10:17:46

标签: sql

我有两个表,我从中提取数据以显示在amcharts中。一个是packagePurchases,另一个是productPurchases。这是我第一次加入桌子,尽管我已经完成了所有阅读,但我遗漏了一些东西。问题是,如果我今天运行报告,如果没有packagePurchases,但有productPurchases,则会忽略productPurchases。

这是我的代码:

$selectDaysQuery = "SELECT productPurchases.purchaseDate, packagePurchases.purchaseDate 
                            FROM productPurchases INNER JOIN packagePurchases
                            ON productPurchases.purchaseDate = packagePurchases.purchaseDate
                            WHERE productPurchases.companyId = '".mysqli_real_escape_string($con, $companyId)."' 
                            AND productPurchases.purchaseDate >= '".mysqli_real_escape_string($con, $dateFrom)."' 
                            AND productPurchases.purchaseDate <= '".mysqli_real_escape_string($con, $dateTo)."'
                            GROUP BY productPurchases.purchaseDate
                            ";

        if($selectDaysResult = $con->query($selectDaysQuery))
        {

            if($selectDaysResult->num_rows>0)
            {

                while($selectDaysRow = $selectDaysResult->fetch_assoc())
                {

                    $date = $selectDaysRow['purchaseDate'];

                    $joinQuery = "SELECT SUM(productPurchases.itemCost) AS productSales, 
                                         SUM(packagePurchases.packageCost) AS packageSales, 
                                             productPurchases.purchaseDate AS category 
                                         FROM productPurchases INNER JOIN packagePurchases 
                                         ON productPurchases.purchaseDate = packagePurchases.purchaseDate 
                                         WHERE productPurchases.purchaseDate = '".$date."'
                                         GROUP BY productPurchases.purchaseDate, packagePurchases.purchaseDate 
                                         ORDER BY packagePurchases.purchaseDate DESC";

                    if($joinResult = $con->query($joinQuery))
                    {

                        while($joinRow = $joinResult->fetch_assoc())
                        {

                            $data[$x]['category'] = $joinRow['category'];
                            $data[$x]['productSales'] = number_format($joinRow['productSales'], 2, '.', ',');
                            $data[$x]['packageSales'] = number_format($joinRow['packageSales'], 2, '.', ',');
                            $x++;
                        }

                    } else {

                        $data['errors'][$e] = mysqli_error($con);
                        $e++;

                    }

                }


            } else {

                $data['nodata'] = true;

            }       

        } else {

            $data['errors'][$e] = mysqli_error($con);
            $e++;


        }

1 个答案:

答案 0 :(得分:0)

查看您的示例,如果至少有一次产品购买或购买套餐,我觉得您需要查看特定日期的结果。这看起来像FULL OUTER JOIN的情况,因为您希望两个表中的所有行都显示在结果中。

INNER JOIN将做什么,只考虑两个表中存在的行。