3
Name    A       B        C         AA   BB  cc
--------------------------------------------------------
Name1   Data1   n       Data3       3   n   15
Name2   n       Data2   Data4       n   2   8

仅当 AA、BB 或 CC 不为空时,我才想从一行中选择 A、B 或 C。

所以结局会出现:

select Name, A,B,c from table where ??? AA<10 and BB<10 and CC<10
^I know this is dumb, just to help you understand my goal.

Name   A      B      C
-------------------------
Name1 Data1
Name2        Data2 Data4

另一个例子:

select Name, A,B,c from table where ??? AA<5 and BB<5 and CC<5
^I know this is dumb, just to help you understand my goal.

Name   A      B      C
-------------------------
Name1 Data1
Name2        Data2

再举一个例子,踢球:

select Name, A,B,c from table where ??? AA<3 and BB<3 and CC<3
^I know this is dumb, just to help you understand my goal.

Name   A      B      C
-------------------------
Name2        Data2

谢谢阅读!

这是我根据您的帮助提出的解决方案,以防万一有人需要:

SELECT `Name`,
CASE
    WHEN SIGN(`AA`)>0
    THEN `A`
END A,
CASE
    WHEN SIGN(`BB`)>0
    THEN `B`
END B,
CASE
    WHEN SIGN(`CC`)>0
    THEN `C`
END C,
FROM TABLE;
4

1 回答 1

4

你的意思是喜欢使用IS NOT NULL

select Name, A,B,c from table where 
    AA IS NOT NULL AND BB IS NOT NULL 
    AND CC IS NOT NULL

或者

SELECT Name, 
CASE WHEN AA IS NOT NULL 
    THEN A ELSE NULL AS acol,
CASE WHEN BB IS NOT NULL
    THEN B ELSE NULL AS bcol,
CASE WHEN CC IS NOT NULL
    THEN C ELSE NULL AS ccol
FROM table WHERE your_other_conditions
于 2013-08-18T05:09:26.990 回答