I have the following problem: I have a table called FOOD with a column "NAME" containing name of foods, such as "Rice", "White Rice" , "Milk", "Soy Milk" etc.
I need to filter out the rows which have a NAME containing only a single word, and then count in how many rows each of these single words appears in the NAME column as a part of a name of a food.
For example if the NAME column is: "Rice", "White Rice" , "Milk", "Soy Milk", "Brown Rice", Then the result should be Rice - count is 3 and Milk - count is 2.
I know how to get the values in NAME column which contain a single word:
SELECT NAME AS lones FROM FOOD WHERE NAME NOT LIKE '% %' GROUP BY lones
How do I continue from here?
Thanks
Try this query. I JOIN food
against a temp table consisting of single-word foods (which I create by selecting names without space in them).
The JOIN condition is that the single-word food is present in the joined food table's name column.
POSITION() works like indexOf() function in other languages. It returns 0 if the search string is not found in the source string.
select single_word_foods.name, count(*)
from food INNER JOIN
(select distinct name from food where position(' ' in name) = 0) as single_word_foods
on POSITION(single_word_foods.name in food.name) > 0
group by single_word_foods.name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.