Reputation: 352
I have a table with the following columns: id, firstname, lastname, category. And let's say i have data with categories like: action, comedy, drama, horror. My query is
SELECT * FROM tablename WHERE firstname LIKE '%John%'
The query will bring all the records that contain 'John' in firstname, and let's assume that the records have different values for categoty: action and comedy only.
How to find out the list with all the values available in category column for this specific search.
Thanks!
Upvotes: 0
Views: 124
Reputation: 7139
If you want the category list with a count on the number of occurrencies:
SELECT DISTINCT CATEGORY, COUNT(*) AS Total FROM tablename WHERE firstname LIKE '%John%' GROUP BY CATEGORY ORDER BY CATEGORY
Upvotes: 0
Reputation: 15569
If you want a distinct list of categories, where firstname is like John:
SELECT DISTINCT category FROM tablename WHERE firstname LIKE '%John%'
Upvotes: 3
Reputation: 1
try this:
SELECT *
FROM tablename
WHERE firstname LIKE '%John%'
and category='category_name'
Upvotes: 0
Reputation: 38
SELECT category FROM tablename WHERE firstname LIKE '%John%'
Simple as it looks
Upvotes: 0