简体   繁体   English

如何从MySQL查询结果的特定列中查找值?

[英]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. 我有一个包含以下列的表格:ID,名字,姓氏,类别。 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. 该查询将带所有名字中包含“ John”的记录,并假设这些记录的类别值不同:仅动作和喜剧。

How to find out the list with all the values available in category column for this specific search. 如何针对此特定搜索在类别列中找到所有可用值的列表。

Thanks! 谢谢!

如果您想要一个不同的类别列表,则名字类似于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%' 

看起来很简单

如果您希望类别列表中包含出现次数:

SELECT DISTINCT CATEGORY, COUNT(*) AS Total FROM tablename WHERE firstname LIKE '%John%' GROUP BY CATEGORY ORDER BY CATEGORY

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建MYSQL查询以获取特定结果(从一栏中计算所有唯一值) - How to create MYSQL query to get specific results (count all unique values from one column) 使用 php 在 mysql 查询的结果中查找特定值 - Use php to find specific values in results of mysql query 我如何确定mysql_query是否死掉而没有返回结果 - How do I find out if no results are returned from a mysql_query w/out dieing 如何自动找出PHP中特定MySQL列的数字索引? - How to find out automatically the numeric index of an specific MySQL column in PHP? 如何在PHP中从mysql查询中计数数组值并将第二列与计数结果相关联 - How can I count array values from mysql query in PHP and associate a second column with the count results 如何使用MySQL查询从DB中的所有行中查找类似的结果 - How to find similar results from all rows in DB with MySQL query 如何使用 MySQL 查询从同一列获得 2 个不同的结果 - how to get 2 different results from the same column using MySQL Query PHP / MySQL-MySQL查询以按特定列对结果进行排序 - PHP / MySQL - MySQL query to sort results per specific column 如何编写Mysql查询以从特定列中选择数据? - How to write a Mysql Query to select data from specific column? MySQL查询从数据库表中的所有字符串中查找特定区域数据并列出所有相关的标题 - MySQL query to find out specific region data from all strings in database table and list all related banners
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM