如何在MySQL查询的结果中找出特定列的值?
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!
我有一个包含以下列的表:id,firstname,lastname,category。 让我们说我有数据 与类别类似:动作,喜剧,戏剧,恐怖。 我的查询是 p>
SELECT * FROM tablename WHERE firstname LIKE'%John%'
code> pre>
查询将带来名字中包含“John”的所有记录,并假设这些记录对于Categoty具有不同的值:仅限于动作和喜剧。 p>
如何找到包含此特定搜索的类别列中所有可用值的列表。 p>
谢谢! p>
div>
If you want a distinct list of categories, where firstname is like John:
SELECT DISTINCT category FROM tablename WHERE firstname LIKE '%John%'
try this:
SELECT *
FROM tablename
WHERE firstname LIKE '%John%'
and category='category_name'
SELECT category FROM tablename WHERE firstname LIKE '%John%'
Simple as it looks
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