I'm trying to count several items in the same table below:
| food |
| ------------ |
| apple cider |
| cake manzana (manzana in spanish == apple) |
| carrot cake |
| banana pudding |
I want end up with the following table:
food | count |
---|---|
apple | 2 |
carrot | 1 |
banana | 1 |
I tried the following query which works for counting the apples:
SELECT 'apple' as placeholder, COUNT(*)
FROM table WHERE (food LIKE '%apple%' OR food LIKE '%manzana%') and date > '10/10/2021'
The issue is that when I try to expand it to get all of the food items together, it takes a long while:
SELECT 'apple' as placeholder, COUNT(*) FROM table WHERE (food LIKE '%apple%' OR food LIKE '%manzana%') and date > '10/10/2021' and ...
UNION ALL
SELECT 'carrot' as placeholder, COUNT(*) FROM table WHERE food LIKE '%carrot%' and date > '10/10/2021' and ...
UNION ALL
SELECT 'banana' as placeholder, COUNT(*) FROM table WHERE food LIKE '%banana%' and date > '10/10/2021' and ...
Any other way to construct this query?
2条答案
按热度按时间ars1skjm1#
Load the foods you're looking for into a temp table, then join with
LIKE
:c9qzyr3d2#
Why don't you do :
--> example :
--> results :