我正在运行一个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`
如果我在获得260个结果的过程中添加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 (REPLACE( (REPLACE( s.NAME, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", ''))AND (REPLACE( (REPLACE( nc.OrganisationName, "'", '')), " ", '')) LIKE (REPLACE( (REPLACE( es.site_name, "'", '')), " ", ''))
现在我想做的是找到不在where子句中的所有结果。对我来说,第一个显而易见的解决方案是将其括在方括号中并在其前面加上NOT
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, "'", '')), " ", '')))
答案 0 :(得分:3)
您有一些复杂的newObj.data
表达式,但这没关系。重要的是:的反函数:
WHERE
不是
WHERE <something>
这是因为表达式可以求值为WHERE NOT <something>
,对于外部联接尤其如此。
正确的逆是:
NULL