假设我有序列号,测试名称和其他一些列,我想为特定序列号编写TESTNAME is null
条件,然后将TESTNAME
设置为空,否则执行内连接
SELECT
(A.PTNUMBER + '-' +A.SL_NO) AS ENUMBER,
D.ENGINEER AS REQ, D.DATETIME as "DATE",
(select Value
from DROPDOWN
where B.TEST_NAME=CONVERT(VARCHAR,DropdownID)) TESTNAME,
TABLE_NAME AS TABLETD
FROM INSPECTION D
INNER JOIN TABLEA A ON D.ENGID = CONVERT(VARCHAR,A.EN_ID)
INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR,A.EN_ID)
INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR,A.EN_ID)
答案 0 :(得分:0)
不确定将testname设置为空白是什么意思,但如果您打算使用SELECT
查询,那么您可以这样做
select *,
case when TESTNAME is null and serial_number = some_value then '' end as TESTNAME
from mytable
答案 1 :(得分:0)
您可以将case
表达式和coalesce()
与您的联接结合使用,以选择要返回的值。
select serial_number, ...
,case when coalesce(testname,'') <> ''
then t2.testname
else coalesce(testname,'') end
from t
inner join t2
on ...
您可以在sql server中使用isnull()
或coalesce()
来返回不同的值来替换null
。
select isnull(testname,'')
或
select coalesce(testname,'')
两者之间的主要区别在于coalesce()
可以支持2个以上的参数,并选择第一个不是null
的参数。两者之间的差异更大answered here.
select coalesce(testname,testname2,'')
coalesce()
也是标准的ANSI sql,因此您可以在大多数RDBMS中找到它。 isnull()
特定于sql server。
参考:
答案 2 :(得分:0)
SELECT (A.PTNUMBER + '-' + A.SL_NO) AS ENUMBER,
D.ENGINEER AS REQ,
D.DATETIME as "DATE",
case
when SerialNo = xxx and TESTNAME is null then ''
else (select Value from DROPDOWN where B.TEST_NAME = CONVERT(VARCHAR, DropdownID))
end AS TESTNAME,
TABLE_NAME AS TABLETD
FROM INSPECTION D
INNER JOIN TABLEA A ON D.ENGID = CONVERT(VARCHAR, A.EN_ID)
INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR, A.EN_ID)
INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR, A.EN_ID);