Raluca Albu
Raluca Albu

Reputation: 352

How to find out the values from a specific column in the results of a MySQL query?

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

Answers (4)

LS_
LS_

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

Hooman Bahreini
Hooman Bahreini

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

Guliver
Guliver

Reputation: 1

try this:

SELECT * 
FROM tablename 
WHERE firstname LIKE '%John%' 
and category='category_name'

Upvotes: 0

Ashutosh Panda
Ashutosh Panda

Reputation: 38

SELECT category FROM tablename WHERE firstname LIKE '%John%' 

Simple as it looks

Upvotes: 0

Related Questions