我有兩個MySQL表:SQL計數有不匹配的記錄在一個一對多的關係
調查(日期,地點,公里) 主鍵:時間+地點 (每調查一個記錄)
標本(日期,地點,種類)(零所或多個記錄的每調查日期和地點)
我想找到調查的數量和受訪公里的總和,其中標本表包含任何記錄特定物種。換句話說,沒有找到特定物種的調查數量。
調查的總數爲:
發現在沒有標本發現調查的數量很簡單:
select count(s.date) as surveys, sum(s.kilometers) as KM_surveyed
from surveys=s left join specimens=p
on (s.date=p.date and s.location=p.location)
where p.date is null;
+---------+-------------+
| surveys | KM_surveyed |
+---------+-------------+
| 8820 | 15848.26 |
+---------+-------------+
的標本中記錄的總數爲:
select count(*) from specimens;
+-----------+
| count(*) |
+-----------+
| 51566 |
+-----------+
上的所有調查,結果發現斑海豹(HASE)的正確號碼是:
select count(*) from specimens where species = 'HASE';
+-----------+
| count(*) |
+-----------+
| 662 |
+-----------+
找到發現Harbor Seals(HASE)的調查數量並不容易。在沒有斑海豹(HASE)被發現發現調查的數量
select count(s.date), sum(s.kilometers)
from surveys=s
left join specimens=p on (s.date=p.date and s.location=p.location)
where p.species = 'HASE';
+---------+-------------+
| surveys | KM_surveyed |
+---------+-------------+
| 662 | 2030.70 | WRONG! that is number of specimens not surveys
+---------+-------------+
:
由於標本表通常包含每調查多條記錄,該查詢返回不調查的數量卻發現HASE的樣本數目也不容易。該查詢返回不調查的電話號碼,但是沒有斑海豹發現的標本數量:
select count(s.date), sum(s.kilometers)
from surveys=s
left join specimens=p on (s.date=p.date and s.location=p.location)
where p.species <> 'HASE' or p.date is null;`
+---------+-------------+
| surveys | KM_surveyed |
+---------+-------------+
| 50904 | 151310.49 |
+---------+-------------+
錯了! 50904 =非HASE數標本
如何構造查詢到正確計算,其中斑海豹被發現調查的數量和調查計數時,並沒有發現他們?
我想可能會丟失不同的信息 –
如果您將一個不同的主鍵用於調查以及日期+位置組合以外的其他樣本,那麼這樣做會容易得多,因爲您想查找Harbor Seals所在的調查數量找到/未找到。由於您的樣本表也使用日期+位置,因此您必須決定是否是特定的日期或時間,您將考慮「位置」。這導致更復雜的問題/查詢超出必要。 –
這是什麼:'從調查= s左加入標本= p'?你不能在表名和別名之間加'='。 – Barmar