我运行一个SQL查询,告诉我一些表结果。没有WHERE子句的查询返回结果1087
SELECT hf.id, hf.`nhs_choices_organisationid`,hf.`sites_organisation_code`,hf.`eric_site_id` FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
如果我添加在WHERE子句中,我得到260个结果
SELECT hf.id, hf.`nhs_choices_organisationid`,hf.`sites_organisation_code`,hf.`eric_site_id` FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
WHERE (REPLACE( (REPLACE( s.NAME, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", ''))AND (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( es.site_name, "'", '')), " ", ''))
现在我想做的是找到所有未在where子句中的结果。我的第一个明显的解决方案是围绕在支架和不带它的前缀
SELECT hf.id, hf.`nhs_choices_organisationid`,hf.`sites_organisation_code`,hf.`eric_site_id` FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
WHERE NOT ((REPLACE( (REPLACE( s.NAME, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", ''))AND (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( es.site_name, "'", '')), " ", '')))
但是,这只是返回370个结果
我试图做它明确这个时候
SELECT hf.id, hf.`nhs_choices_organisationid`,hf.`sites_organisation_code`,hf.`eric_site_id` FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
WHERE (REPLACE( (REPLACE( s.NAME, "'", '')), " ", '')) NOT LIKE (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", ''))OR (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", '')) NOT LIKE (REPLACE( (REPLACE( es.site_name, "'", '')), " ", ''))OR (REPLACE( (REPLACE( es.site_name, "'", '')), " ", '')) NOT LIKE (REPLACE( (REPLACE( s.NAME, "'", '')), " ", ''))
并再次只得到370。
现在,根据我的数学1087-260≠370的基本原理的理解。
我可以把它全部写出来进入返回预期的827,但必要的子查询?
SELECT hf.id, hf.`nhs_choices_organisationid`,hf.`sites_organisation_code`,hf.`eric_site_id` FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
WHERE hf.id NOT IN
(SELECT hf.id FROM `hospital_final` hf
left join nhs_choices nc on nc.OrganisationID = hf.`nhs_choices_organisationid`
left join sites s on s.`Organisation Code` = hf.`sites_organisation_code`
left join eric_site es on es.site_code = hf.`sites_organisation_code`
WHERE (REPLACE( (REPLACE( s.NAME, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", ''))AND (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( es.site_name, "'", '')), " ", '')))
你有一些复杂的WHERE
表达,但这并不重要。什么是重要的是,的倒数:
WHERE <something>
不是
WHERE NOT <something>
这是因为表达式可以计算为NULL
- 这是尤其如此,与外部连接。
正确的反应该是:
WHERE (NOT <something> OR
<something> IS NULL
)