MySQL Left Join (using MeekroDB)

377 Views Asked by At

This is really a MeekroDB exclusive issue, just trying to wrap my head around Left_Join.

I have 2 Tables: comp_checklist and comp_checklist_items

I want to get all the rows from comp_checklist where the user Id matches and this worked fine:

DB::query("SELECT * FROM comp_checklist WHERE user_id = %i", $user_id );

Now I wand to get that same query ( all the rows from comp_checklist where the user Id matches ) and add any comp_checklist_items rows where checklist_id matches in both tables (checklist_id is the primary key in the comp_checklist table). I used below but just get false

DB::query("SELECT * FROM comp_checklist WHERE user_id = %i LEFT JOIN comp_checklist_items on checklist_id = comp_checklist.checklist_id", $user_id );

1

There are 1 best solutions below

2
On

JOIN should come before WHERE, and all columns should have the table name or you will get an error:

DB::query("SELECT * FROM comp_checklist 
           LEFT JOIN comp_checklist_items 
           ON comp_checklist_items.checklist_id = comp_checklist.checklist_id 
           WHERE comp_checklist.user_id = %i ", $user_id );