I am working on an application that will do some sort of smart data search by disabling unrelated tags in a search pool.
For example (sorry if my formatting may be crappy), given this table:
_id | _tagname
1 A
1 B
1 C
2 A
2 B
3 A
4 B
5 C
6 D
when the user select tag A, (the following will be performed on each tag selection):
- Fetch ID’s that will match the tag -> 1, 2, 3 which will now be the new search pool
- Hide all tags that will not be relevant -> D since tag B and C will allow the user to filter the tags to get ID 1
TL;DR: This is my current approach and I was wondering if there are ways to optimize it because currently it is taking too long to get the results
SELECT _tagname FROM datatags WHERE
(_tagname) NOT IN
( SELECT _tagname FROM datatags WHERE
_id IN (1,2,3))
GROUP BY _tagname
Thank you! This is my first time posting a question so please take it easy on me 🙂
Edit: formatting
Are you ultimately looking for the tags that appear together, or do you really want the odd tags out?
Here are two ways to find the related tags (returns
{ A, B, C }forAorBorC, and returns{ D }forD)Use a correlated subquery:
Use a self join:
For the inverse (tags that never appear with the given tag), you’d find the difference between the entire set of tags and the related tags. MySQL doesn’t support
MINUS, but here one way to find the unrelated tags:Use a correlated subquery with a self join: