Shane Shane - 4 months ago 15x
MySQL Question

Laravel 4 - Select rows where count of rows in joined table greater than 0

I need to grab rows of

s where rows of
s exist. Here is my code so far:

$users = DB::table('users')
->join('orders', 'orders.user_id', '=', '')
->join('items', 'items.user_id', '=', '');

How do I add a where condition that will only select
s that have at least one


You don't have to add an extra condition. The query will return results ONLY for the users you have orders and items related with. If you want to return only the users that have items related with, you can drop the join with the orders table (even though I have no idea why you would have user_id in the items table instead of in the orders table).

$users = DB::table('users')->join('items', 'items.user_id', '=', '');