开发者

mysql syntax in clause

开发者 https://www.devze.com 2023-03-01 04:15 出处:网络
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 (

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消