I have a keywordsList table has two columns, like this:
id | keywords
-------------
1 | [apple cake, apple pie, apple cookie]
-------------
2 | [banana cake, banana juice]
-------------
3. | [orange candy]
I want to query the string which match anything in keywords columns and count how many result that I search successfully.
For example, I search "apple", and I want to get the result like this:
id | keywords | totalCount
-------------------------------------------------
1 | [apple cake, apple pie, apple cookie] | 1
I want to get the result shows that which array match "apple", and how many rows I got.
This's code that I try to get that result:
SELECT DISTINCT "id", "keywords", COUNT("id") OVER () as totalCount
FROM (SELECT DISTINCT *, unnest("keywords") AS "unnestKeywords" FROM "keywordsList") AS "keywordsList"
WHERE "unnestKeywords" ILIKE "%apple%"
But I get the result like this:
id | keywords | totalCount
-------------------------------------------------
1 | [apple cake, apple pie, apple cookie] | 3
I could get correct id and keywords columns, but couldn't get correct count.
Hope to get any suggestion. Thanks
COUNT(DISTINCT "id"), BTW can you make certain to post correct queries because I highly doubt"apple"is correct shouldn't that be'%apple%'"apple"won't work.'cake'?cake, I want get the result with 2 rows and count 2.