Sql server 如何使用SQL从表列中仅检索小于给定值的值
如果余额小于500,我必须对只返回行进行查询。但是仍然得到了SQL表的所有行 我的问题是:Sql server 如何使用SQL从表列中仅检索小于给定值的值,sql-server,tsql,Sql Server,Tsql,如果余额小于500,我必须对只返回行进行查询。但是仍然得到了SQL表的所有行 我的问题是: SELECT a.[ID], a.[strUserName], (SELECT intCurrentBalance FROM accountmappings WHERE intUserID = a.[ID] AND intAccountType = 1 AND (intCurrentBalance < 500 AND intCurrentB
SELECT
a.[ID], a.[strUserName],
(SELECT intCurrentBalance
FROM accountmappings
WHERE intUserID = a.[ID] AND intAccountType = 1
AND (intCurrentBalance < 500 AND intCurrentBalance IS NOT NULL)) AS Prm_Balance,
(SELECT intCurrentBalance
FROM accountmappings
WHERE intUserID = a.[ID] AND intAccountType = 2
AND (intCurrentBalance < 500 AND intCurrentBalance IS NOT NULL)) AS Trn_Balance,
(SELECT intCurrentBalance
FROM accountmappings
WHERE intUserID = a.[ID] AND intAccountType = 3
AND (intCurrentBalance < 500 AND intCurrentBalance IS NOT NULL)) AS Opt_Balance,
a.[strMobile],
a.[strEmailID],
a.[bIsApproved],
a.[bIsActive],
a.[dtlastrecharge],
CASE WHEN b.id IS NULL THEN 0 ELSE b.id END AS createdbyId,
CASE WHEN b.strusername IS NULL THEN '' ELSE b.strusername END AS createdby
FROM
users a
LEFT JOIN
users b ON a.intCreatedBy = b.ID
您的查询应该返回它没有where子句的所有行。只有相关子查询检查余额是否小于500。如果找不到合适的值,这些子查询将返回NULL。提示:在子查询中使用表别名,并将其应用于所有列引用。@HABO-你能用一些代码解释一下吗…我不明白什么是CB…@shalingajjar CB是intCurrentBalance表的对象
SELECT a.[ID] ,a.[strUserName],
(CASE WHEN CB.intAccountType = 1 THEN CB.intCurrentBalance else 0) as Prm_Balance
,(CASE WHEN CB.intAccountType = 2 THEN CB.intCurrentBalance else 0) as Trn_Balance
,(CASE WHEN CB.intAccountType = 3 THEN CB.intCurrentBalance else 0) as Opt_Balance
,a.[strMobile]
,a.[strEmailID]
,a.[bIsApproved]
,a.[bIsActive]
,a.[dtlastrecharge]
,case when b.id is null then 0 else b.id end as createdbyId
,case when b.strusername is null then '' else b.strusername end as createdby
FROM users a
left join users b on a.intCreatedBy = b.ID
inner join intCurrentBalance CB on intUserID = a.[ID] and (CB.intCurrentBalance IS NOT NULL and CB.intCurrentBalance < 500)