Shane Shane - 1 year ago 145
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

Answer Source

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', '=', '');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download