我有一张桌子
farmid chickens cows horses
10 104 50 2
12 62 110 7
17 74 12 154
我想查询表,只显示大于100的值。输出应该如下所示
farmid chickens cows horses
10 104
12 110
17 154
有没有人对如何使这项工作有任何建议?
我试过这个:
SELECT farmid, chickens, cows, horses
FROM farms
WHERE chickens >=100 OR cows>=100 OR horses>=100;
然而,返回
farmid chickens cows horses
10 104 50 2
12 62 110 7
17 74 12 154
答案 0 :(得分:4)
如果你想让价值低于100的鸡,牛,马细胞为空或空,你应该使用case ... when。
例如......
select
(case when chickens > 100 then chickens else null end case) as chickens,
(case when cows > 100 then cows else null end case) as cows,
(case when horses > 100 then horses else null end case) as horses
from table_name
where
chickens > 100 OR cows > 100 OR horses > 100
答案 1 :(得分:1)
您需要WHERE子句。
语法:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
您可以使用OR和> WHERE子句中的符号以获得所需的结果。
SELECT farmid,
CASE WHEN chickens > 100 THEN chickens ELSE NULL
END as chickens,
CASE WHEN cows > 100 THEN cows ELSE NULL
END as cows,
CASE WHEN horses > 100 THEN horses ELSE NULL
END as horses,
from tableName
WHERE chickens > 100 OR cows > 100 OR horses > 100
这将选择farmid,并且取决于值>>的情况。 100,它将选择该列值。即使多个列具有值> gt,它也应该工作。 100。
答案 2 :(得分:1)
这将返回所有表格行,如果您只想要chickens
,cows
或horses
中的至少一个>> = 100的行,请将最后一行包含为好。
SELECT
farmid,
CASE
WHEN chickens >= 100 THEN chickens
ELSE NULL
END AS chickens,
CASE
WHEN cows >= 100 THEN cows
ELSE NULL
END AS cows,
CASE
WHEN horses >= 100 THEN horses
ELSE NULL
END AS horses
FROM
<enter table name here>
WHERE chickens >= 100 OR cows >= 100 OR horses >= 100