我想知道SQL查询的where子句中的哪个条件失败 例如,我有以下声明
SELECT * from Users where status != 'inactive'
AND ptype != 'test' AND ptype != 'test1'
AND (p_name NOT IN ('ptest', 'ptest0', 'ptest1', 'ptest2') OR p_name IS NULL)
AND (p_m_c NOT LIKE '%pmcestt%'
OR (ptype LIKE 'SomeOthertest%' AND p_m_c IS NULL)
OR (TType = 'Broad' AND p_m_c IS NULL)
OR p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8'))
我如何知道哪种情况失败,并获得该条件?
答案 0 :(得分:0)
如果要检查条件,则必须将WHERE
子句中的条件语句移动到SELECT
列表中除条件用户列之外的每个条件的CASE语句。
SELECT *,
CASE WHEN status !='inactive' THEN 1 else 0 end as statusTag,
CASE WHEN ptype !='test' THEN 1 ELSE 0 end as ptypeTag,
.
.
CASE WHEN p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8') THEN 1 else 0 end as p_m_cTag
FROM Users
这将为每个表行生成列的列表,如果满足条件则指示值1,如果该特定行的条件为假,则为0。