Hi, I need some help with this problem. I work in a web application and I use sqlite for the database. Can someone help me with one query from the database, which should be optimized == fast =)
I have a table x:
ID | ID_DISH | ID_INGREDIENT
1 | 1 | 2
2 | 1 | 3
3 | 1 | 8
4 | 1 | 12
5 | 2 | 13
6 | 2 | 5
7 | 2 | 3
8 | 3 | 5
9 | 3 | 8
10| 3 | 2
.... ID_DISH is the identifier of different dishes, ID_INGREDIENT is the ingredient from which the dish is made: therefore, in my case, the dish with identifier 1 is made with ingredients with idi 2,3
There are over 15,000 rows in this table, and my question is:
I need a query that will retrieve strings where I can find the identifiers of dishes sorted by count of the number of ASC debris that I added to my algorithm.
examle: foo (2,4) there will be lines in the following order:
ID_DISH | count(stillMissing)
10 | 2
1 | 3
A dish with id 10 contains ingredients with id 2 and 4 and does not yet have 2, then
My request:
SELECT
t2.ID_dish,
(SELECT COUNT(*) as c FROM dishIngredient as t1
WHERE t1.ID_ingredient NOT IN (2,4)
AND t1.ID_dish = t2.ID_dish
GROUP BY ID_dish) as c
FROM dishIngredient as t2
WHERE t2.ID_ingredient IN (2,4)
GROUP BY t2.ID_dish
ORDER BY c ASC
, ....