我正在努力让我的 SQL 查询正常工作。当我无法找到一行时,我想返回一行,就像图片中的行一样,但我希望将 ifuserholdscertificate 设置为“否”。
SELECT tuc.id,
tu.firstName,
tuc.uid,
tuc.value,
tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) AS 'amount',
'certificate DESIRABLE' AS 'typeofthing' ,
'YES' AS 'HasorNot',
ifuserholdscertificate = CASE
WHEN count(tuc.value) = 0 THEN 'NO'
ELSE 'YES'
END
FROM t_user_certificates tuc,
t_certificates tc,
t_users tu
WHERE tuc.value IN (4,
12,
31)
AND tuc.value = tc.id
AND tu.id = tuc.uid
GROUP BY tuc.id,
tu.firstName,
tuc.uid,
tuc.value,
tc.name
这是查询生成的数据!
正如您所看到的,即使有些人在金额行中只得到 2,它仍然不会获取一行并将 ifuserholdscertificate 设置为“否”。
根据要求更新!
select tuc.id,
count(tuc.value) as 'counten',
tu.firstName,
tuc.uid,
tuc.value,
tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) as 'amount',
'certificateDESIRABLE' as 'typeofthing' ,
'YES' as 'HasorNot',
HasOrders = CASE
WHEN count(tuc.value) = 0 THEN 'NO'
ELSE 'YES'
END
from t_user_certificates tuc
left outer join t_certificates tc
on tuc.value = tc.id
left outer join t_users tu
on tu.id = tuc.uid
GROUP BY tuc.id, tu.firstName, tuc.uid, tuc.value, tc.name
计数中总是一个 1 并且总是“是”