Retrieving a User’s Listings by Category

Problem

I need to retrieve all of a specified user’s listings that are in a specific category.

Solution

SELECT title,listing_id, description, category FROM listing WHERE
 category = $category AND poster = $user;

Discussion

Unfortunately, there’s no way to retrieve all of the listings in a category independently of the poster, as you ultimately need a WHERE clause that acts on either the poster or listing_id fields.

You can easily modify this recipe to search for all of the listings in a category from all of the user’s friends:

SELECT title, url, description, price category FROM listing
 WHERE category = $category AND poster IN (SELECT uid2 FROM friend
 WHERE uid1 = $user);

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.