Skip to content
Advertisement

Only count SQL entry if two rows (as a pair) have the needed conditions

How can I only count the “Great” per “Country” in this SQL table?

My table “commentmeta

comment_id meta_key meta_value
540 Voting Great
540 Country UK
560 Voting Great
560 Country PL
610 Voting Bad
610 Country UK
630 Voting Great
630 Country UK

The result should be UK: 2 times “Great”, PL: 1 time “Great”

With something like this I just get the allover count (all “Greats”, not per country):

How to combine it with the “comment_id” to get the connection between “Voting” & “Country”, so that I can count it per country?

Advertisement

Answer

You may try the following to get all counts:

Also, you may try the following to get the count for a specific country:

See a demo from db<>fiddle.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement