1

我在这样的表中有行Players

+-----------+----+----+----+----+----+----+----+----+
|  USERNAME | A  | B  | C  | D  | E  | F  | G  | H  |
+-----------+----+----+----+----+----+----+----+----+
|   Mike    | 45 | 34 | 56 | 58 | 29 | 74 | 39 | 48 |
+-----------+----+----+----+----+----+----+----+----+

现在我的查询应该有点像..

SELECT USERNAME FROM PLAYERS 
IF 
 (DIFFERENCE BETWEEN A & B <20)

IF
 (DIFFERENCE BETWEEN C & D <20)

IF
 (DIFFERENCE BETWEEN E & F <20)
IF
 (DIFFERENCE BETWEEN G & H <20)

现在..如果四个条件中的任何两个为真,我必须选择用户名..请帮助

4

2 回答 2

1

假定 SQL Server

select
   username 
from(
    SELECT USERNAME,
        case when abs(a-b)<20 then 1 else 0 end A
        case when abs(c-d)<20 then 1 else 0 end b
        case when abs(e-f)<20 then 1 else 0 end c
        case when abs(g-h)<20 then 1 else 0 end d
    FROM PLAYERS
)tmp
WHERE tmp.a+tmp.b+tmp.c+tmp.d>=2
于 2013-02-09T02:03:15.080 回答
1

假设mysql:

select username from (
    select username,
        if(b-a < 20, 1, 0) as ab,
        if(d-c < 20, 1, 0) as cd,
        if(f-e < 20, 1, 0) as ef,
        if(h-g < 20, 1, 0) as gh
    from players
) x where ab + cd + ef + gh = 2

abs()如果 a 可以高于 b,您可能想要使用。>= 2如果两个或多个条件都很好,您可能想要使用。

于 2013-02-09T02:04:52.177 回答