Php MySQL查询不检查条件

Php MySQL查询不检查条件,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我有这样一个问题: select * from `research_purchases` left join `company_research_articles` on `research_purchases`.`researchable_id` = `company_research_articles`.`id` and `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities \Compan

我有这样一个问题:

    select * from `research_purchases` left join `company_research_articles`
 on `research_purchases`.`researchable_id` = `company_research_articles`.`id` 
and `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities
\CompanyResearchArticle'
调研表结构如下:

它不会过滤Modules\Analystsweb\Entities\companyresearchararticle部分并给出整个结果。 这就是我被困的地方

最初我想运行一个Laravel查询。 查询如下:

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
                                        $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id')
                                            ->where('research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle');
                                    })
                                    ->where('research_purchases.created_at', '>', Carbon::now()->subMonths(11))
                                    ->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                                    ->get();

如有任何建议,将不胜感激。谢谢。

哦,亲爱的,你应该把这个放在何处

若你们想在右表的where条件下左连接所有数据,那个么你们就把它和join放在一起


我还不能发表评论,但你的意思是在哪里而不是在你的查询中?

请尝试whereColumn

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
               $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id');
        })->whereColumn([
                    ['research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle'],
                    ['research_purchases.created_at', '>', Carbon::now()->subMonths(11)]
        ])->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                             ->get();
或者两者在哪边越近

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
                                        $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id')
                                        ->where(
                    'research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle')
                             ->where('research_purchases.created_at', '>', Carbon::now()->subMonths(11))
                )
})->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                                    ->get();

如果我添加where而不是where,我不会得到任何结果。它会给出零结果。而不是检查你的where条件你有完全像这样的数据如果你给我查询,我可以帮你。但是我不知道Laraveth这个查询位于帖子的顶部,请检查。我已经给了你一个理由检查其中一条记录的长度,确保长度与限制相同51您可能有一个非显示字符导致问题。空格、制表符、行尾等
$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
                                        $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id')
                                        ->where(
                    'research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle')
                             ->where('research_purchases.created_at', '>', Carbon::now()->subMonths(11))
                )
})->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                                    ->get();