如果1个表缺少数据,则sqlite查询无法获取所有记录

时间:2015-05-07 15:59:19

标签: sql database sqlite

我有一个非常复杂的数据库,在SQLite中有很多表。我试图设计一个查询来报告这些表中的大量数据,并报告那些可能在一个或多个表中没有记录的绵羊。

我的查询是:

SELECT sheep_table.sheep_id, 
(SELECT tag_number FROM id_info_table WHERE official_id = "1" AND id_info_table.sheep_id = sheep_table.sheep_id AND  (tag_date_off IS NULL or tag_date_off = '')) AS fedtag, 
 (SELECT tag_number FROM id_info_table WHERE tag_type = "4" AND id_info_table.sheep_id = sheep_table.sheep_id AND  (tag_date_off IS NULL or tag_date_off = '')) AS farmtag,
 (SELECT tag_number FROM id_info_table WHERE tag_type = "2" AND id_info_table.sheep_id = sheep_table.sheep_id AND (tag_date_off IS NULL or tag_date_off = '') and ( id_info_table.official_id is NULL or id_info_table.official_id = 0 )) AS eidtag,
 sheep_table.sheep_name, codon171_table.codon171_alleles, sheep_ebv_table.usa_maternal_index,  sheep_ebv_table.self_replacing_carcass_index, cluster_table.cluster_name, sheep_evaluation_table.id_evaluationid,
(sheep_table.birth_type +
sheep_table.codon171 +
sheep_evaluation_table.trait_score01 +
sheep_evaluation_table.trait_score02 +
sheep_evaluation_table.trait_score03 +
sheep_evaluation_table.trait_score04 +
sheep_evaluation_table.trait_score05 +
sheep_evaluation_table.trait_score06 +
sheep_evaluation_table.trait_score07 +
sheep_evaluation_table.trait_score08 +
sheep_evaluation_table.trait_score09 +
sheep_evaluation_table.trait_score10 +
(sheep_evaluation_table.trait_score11 / 10 )) as overall_score, sheep_evaluation_table.sheep_rank, sheep_evaluation_table.number_sheep_ranked,
sheep_table.alert01,
sheep_table.birth_date, sheep_sex_table.sex_abbrev, birth_type_table.birth_type,
sire_table.sheep_name as sire_name, dam_table.sheep_name as dam_name 
FROM sheep_table 
 join codon171_table on sheep_table.codon171 = codon171_table.id_codon171id 
 join sheep_cluster_table on sheep_table.sheep_id = sheep_cluster_table.sheep_id
 join cluster_table on cluster_table.id_clusternameid = sheep_cluster_table.which_cluster
 join birth_type_table on sheep_table.birth_type = birth_type_table.id_birthtypeid 
 join sheep_sex_table on sheep_table.sex = sheep_sex_table.sex_sheepid 
 join sheep_table as sire_table on sheep_table.sire_id = sire_table.sheep_id
 join sheep_table as dam_table on sheep_table.dam_id = dam_table.sheep_id
left outer join sheep_ebv_table on sheep_table.sheep_id = sheep_ebv_table.sheep_id
left outer join sheep_evaluation_table on sheep_table.sheep_id = sheep_evaluation_table.sheep_id 
WHERE (sheep_table.remove_date IS NULL or sheep_table.remove_date is '' ) 
and (eval_date > "2014-10-03%" and eval_date < "2014-11%")
and sheep_ebv_table.ebv_date = "2014-11-01"
order by sheep_sex_table.sex_abbrev asc, cluster_name asc, self_replacing_carcass_index desc, usa_maternal_index desc, overall_score desc

如果给定的绵羊在评估表中没有记录或者在EBV表中没有记录,则不返回记录。我需要返回所有当前动物的所有可用数据,如果它们没有数据,则只留下EBV和评估的字段为空。

我不理解为什么我没有全部获得它们,因为没有一只绵羊拥有所有3种ID类型(联邦,农场和EID),因此这些字段中有空值而我期待的是空值评估总和和ebv字段也是如此。

完全失去了修复它的方法。

1 个答案:

答案 0 :(得分:1)

问题似乎是您在WHERE语句中使用eval_date。我假设eval_date在sheep_evaluation_table中,所以当你在WHERE中使用它时,它会删除eval_date为NULL的任何行,当你使用LEFT OUTER JOIN并且&# 39;在sheep_evaluation_table中没有匹配的记录。

尝试将eval_date过滤器放在连接上,如下所示:

left outer join sheep_evaluation_table on sheep_table.sheep_id = sheep_evaluation_table.sheep_id 
    AND (eval_date > "2014-10-03%" and eval_date < "2014-11%")
WHERE (sheep_table.remove_date IS NULL or sheep_table.remove_date is '' )