Mysql 尝试反转WHERE子句,但它没有';不算

Mysql 尝试反转WHERE子句,但它没有';不算,mysql,sql,Mysql,Sql,我正在运行一个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 jo

我正在运行一个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子句中没有的所有结果。对我来说,第一个显而易见的解决方案是将其括在括号中,并在其前面加上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, "'", '')), " ", '')))

您有一些复杂的
,其中
表达式,但这并不重要。重要的是:

WHERE <something>

您有一些复杂的
,其中
表达式,但这并不重要。重要的是:

WHERE <something>

1) 空处理2)WHERE on column from
LEFT JOIN
使其成为
内部连接。如果不检查空值,则会丢失左联接未产生匹配记录的所有记录。不过,请指向您,以记住
非(a和B)
等于
(非a)或(非B)
。不是每个人都记得这条规则。1)Null处理2)WHERE on column from
LEFT JOIN
使其成为
内部连接。如果不检查空值,则会丢失左联接未产生匹配记录的所有记录。不过,请指向您,以记住
非(a和B)
等于
(非a)或(非B)
。不是每个人都记得这条规则。好的干净的例子(一如既往!)好的干净的例子(一如既往!)
WHERE (NOT <something> OR
       <something> IS NULL
      )