Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
MySQL Query doesn't check for condition

I have a query like this:

    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'

The research_purchases table structure is like this:research_purchases table

It is not filtering the "Modules\Analystsweb\Entities\CompanyResearchArticle" part and giving me the entire result. This is the part where I am stuck.

Originally I want to run a Laravel query. The query is like this:

$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();

Any suggestion would be appreciated. Thank you.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

ohh dear you should put this in where condition

if you want left join all data on where condition of right table then you put it with join on

select * from `research_purchases` left join `company_research_articles`
 on `research_purchases`.`researchable_id` = `company_research_articles`.`id` 

where  `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities
\CompanyResearchArticle'
over 4 years ago · Santiago Trujillo Report

0

I cannot post a comment yet, but don't you mean WHERE instead of AND in your query ?

over 4 years ago · Santiago Trujillo Report

0

Please try 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();

or both the where in side the closer

$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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!