im using mysql and was wondering about its syntax.
i want to make an IN clause something like this:
from_id OR to_id IN (
)
except i dont know how the syntax is
i want to avoid running the in clause twice like this:
from_id IN (
)
OR
to_id IN (
)
best of regards开发者_运维技巧 alexander
You can't do that. Only the second form is valid.
As @judda mentions, don't worry about premature optimization. The query optimizer will take advantage of caching where possible.
If you have some reasons to worry about the IN clause being run twice (for example, if the subquery is particularly complex and the optimiser doesn't seem to have taken advantage of caching the result set), you can always replace the IN clause with INNER JOIN.
So this:
FROM x
WHERE from_id IN (
subquery
)
OR
to_id IN (
subquery
)
becomes this:
FROM x
INNER JOIN (
subquery
) y ON y.value IN (x.from_id, x.to_id)
精彩评论