I’ve just solved an interesting problem using MySQL’s IN, but I don’t know what this “trick” is called.
In my design I needed to filter out rows that either did not require a certain skill, or for which the skill name and the skill level were met. Here’s the solution:
SELECT * FROM table WHERE
skillname = "" OR
(skillname, skilllevel) IN (
SELECT name, level FROM skills WHERE user="Mikhail"
)
I didn’t know that expr IN (value,…), as according to dev.mysql.com both expr and value can be multiple columns.
What is this called, and are there more shortcuts like these?
More importantly – is there a way to convert this query to match skilllevel as “greater than or equal to”?
Solution as per @yokoloko (though he won’t admit it)
SELECT * FROM t1 WHERE
skillname = "" OR
skillname IN (
SELECT name FROM skills WHERE user="Mikhail" AND level >= t1.skilllevel
)
i don’t know how is it called but if I understand well you can do something like this to get the greater than :
But I don’t know if it’s really well optimized.