在mysql查询中使用IN来检索行的替代方法

时间:2021-08-22 09:12:43

In order to retrieve rows from a table in a mysql database, where the column id is equal to 2, 4, 6, 8,....., can someone suggest alternative ways in which these values can be searched for without using IN. I guess we can use OR but is there ant other way?

为了从mysql数据库中的表中检索行,其中列id等于2,4,6,8,.....,有人可以建议可以在不使用IN的情况下搜索这些值的替代方法。我想我们可以使用OR但是有其他方式吗?

1 个解决方案

#1


3  

There are many ways. Some proposals:

有很多方法。一些建议:

  1. Simply: SELECT ... WHERE mod(id, 2) = 0
    In some SQL implementations you can use modulo operator % instead of mod().

    简单地说:SELECT ... WHERE mod(id,2)= 0在某些SQL实现中,您可以使用模运算符%而不是mod()。

  2. Conditional: SELECT CASE mod(id, 2) WHEN 0 THEN ... ELSE ...
    Explanation of SELECT...CASE statement.
    Good example.

    条件:SELECT CASE mod(id,2)WHEN 0 THEN ... ELSE ... SELECT ... CASE语句的说明。好例子。

  3. Regular expression: SELECT ... WHERE id LIKE '%[02468]'
    It returns records with only these id, which are ending with 0, 2, 4, 6 or 8. Even ones.

    正则表达式:SELECT ... WHERE id LIKE'%[02468]'它返回仅包含这些id的记录,这些id以0,2,4,6或8结尾。偶数。

#1


3  

There are many ways. Some proposals:

有很多方法。一些建议:

  1. Simply: SELECT ... WHERE mod(id, 2) = 0
    In some SQL implementations you can use modulo operator % instead of mod().

    简单地说:SELECT ... WHERE mod(id,2)= 0在某些SQL实现中,您可以使用模运算符%而不是mod()。

  2. Conditional: SELECT CASE mod(id, 2) WHEN 0 THEN ... ELSE ...
    Explanation of SELECT...CASE statement.
    Good example.

    条件:SELECT CASE mod(id,2)WHEN 0 THEN ... ELSE ... SELECT ... CASE语句的说明。好例子。

  3. Regular expression: SELECT ... WHERE id LIKE '%[02468]'
    It returns records with only these id, which are ending with 0, 2, 4, 6 or 8. Even ones.

    正则表达式:SELECT ... WHERE id LIKE'%[02468]'它返回仅包含这些id的记录,这些id以0,2,4,6或8结尾。偶数。