table 1 - id,field1
id field1
1 111
2 222
3 333
如果该表中没有条目(table1),我将获得field1的垃圾值。我需要编写一个case语句,如果该表中存在id,它应该返回field1的值,否则为NULL。
例如:如果我选择1,则应给出111。如果id为4则应该为null。
类似
case
// check whether id is in table
if yes
use the value
else
null
。我不知道如何为此编写SQL。
答案 0 :(得分:1)
只需选择具有适当过滤条件的字段:
SELECT field1 FROM table1 WHERE id = ?
在应用程序代码中,您可以通过检查结果集是否为非空来测试是否存在匹配记录。