MySQL query GROUPing from one table, excluding results found in another sub query
-
24-07-2021 - |
Domanda
Let's say I have two tables - ADDONS and DOWNLOADS.
ADDONS dictates which apps come with which addons for FREE.
DOWNLOADS logs addon downloads from all apps, those that include for free and not.
ADDONS
appname | varchar
appaddon | varchar
DOWNLOADS
id | int
appaddon | varchar
appname | varchar
source | varchar
date | datetime
I am trying to write a query that shows the count of each appaddon in DOWNLOADS where it was NOT included for free in ADDONS.
I got as far as: "SELECT appaddon, count(id) FROM downloads GROUP BY appaddon ORDER BY count(id) DESC"
- but that is inclusive of apps that included it for free and those that do not.
Can anyone point me in the right direction on this?
Update:
Example: Imagine I have two holiday themed apps - one for Christmas, one for Halloween, and each app allows you to download costumes that are included with the app (for example Halloween lets you download a witch, skeleton or jack o lantern, and Christmas lets you download a Santa or elf). BUT if someone upgrades from within the app, they can download ANY of the available costumes (so Christmas upgraded could download a skeleton, or Halloween upgraded could download an Elf).
I am trying to get a count of the downloads that are NOT included without upgrading. So I want to exclude the witches downloaded by Halloween, and the Santas downloaded by Christmas, but get a count of the witches, skeletons, elves, etc downloaded by the OTHER apps.
Here is some example data:
ADDONS
appname | appaddon
========================
halloween | jackolantern
halloween | skeleton
halloween | witch
christmas | santa
christmas | elf
christmas | reindeer
easter | bunny
allholidays | bunny
DOWNLOADS
id | appaddon | appname | date
=======================================
1 | skeleton | halloween | 2012-10-15
2 | skeleton | halloween | 2012-10-15
3 | witch | halloween | 2012-10-16
4 | santa | christmas | 2012-10-16
5 | elf | christmas | 2012-10-16
6 | witch | christmas | 2012-10-16
7 | bunny | christmas | 2012-10-16
8 | bunny | halloween | 2012-10-17
9 | bunny | easter | 2012-10-18
Based on above, I would expect the results:
appaddon | count
=================
bunny | 2
witch | 1
Soluzione 3
I think I figured it out! Required me to use WHERE NOT EXISTS.
SELECT appaddon, count(id) FROM DOWNLOADS
WHERE NOT EXISTS (
SELECT appaddon FROM ADDONS
WHERE DOWNLOADS.appname = ADDONS.appname
AND DOWNLOADS.appaddon = ADDONS.appaddon)
GROUP BY appaddon
Fiddler for it here
Altri suggerimenti
SELECT appaddon, count(id) FROM downloads
left join addons on addons.appname=downloads.appname
where addons.appname is null
GROUP BY downloads.appaddon ORDER BY count(id) DESC
This might work for you:
SELECT DOWNLOADS.appaddon, COUNT(*) FROM DOWNLOADS
INNER JOIN ADDONS ON (DOWNLOADS.appaddon = ADDONS.appaddon)
WHERE DOWNLOADS.appname != ADDONS.appname
GROUP BY DOWNLOADS.appaddon;
Here is the SQL Fiddle for it: link