开发者

Statement not returning the correct number or rows

开发者 https://www.devze.com 2023-03-31 00:56 出处:网络
Why is the first statement not returning the number of the 2nd? select random_selection.randnum, random_selection.createddate,

Why is the first statement not returning the number of the 2nd?

select random_selection.randnum,
   random_selection.createddate,
   random_selection.ozip3, 
   random_selection.boxid, 
   random_selection.boxaddr, 
   random_selection.locdesc, 
   random_selection.loccity, 
   random_selection.lastmf,
   random_selection.lastsat,
   random_selection.boxtype,
   random_selection.svcclas,
   random_selection.dropzip5,
   dropper_city_brk_2.dropper_id
from random_selection, dropper_city_brk_2
where random_selection.ozip3 = dropper_city_brk_2.zip3
and dropper_city_brk_2.dropper_id <> 10002
and random_selection.dropper_id is null
order by dropper_id;

Second statement:

select count(*) from random_sel开发者_如何学编程ection where dropper_id is null; 


Your queries are not the same so your record count would not be the same. You first query is joining on 2 tables and your second query is only getting the record count of one table. Plus you have WHERE clauses on your first query that are not on the second.

If you want your second query to return the same number of records then you have to use the same tables and where clause that you have in the first

select count(*) 
from random_selection, dropper_city_brk_2
where random_selection.ozip3 = dropper_city_brk_2.zip3
and dropper_city_brk_2.dropper_id <> 10002
and random_selection.dropper_id is null

EDIT:

select count(*) 
from random_selection 
INNER JOIN dropper_city_brk_2
  ON random_selection.ozip3 = dropper_city_brk_2.zip3
WHERE dropper_city_brk_2.dropper_id <> 10002
  and random_selection.dropper_id is null

Here is a visual explanation of JOINs that might be helpful as a reference.


Do you mean "Why is the second query not returning the number of rows given by the first query"? If so, then it's probably because of the cross join and the extra where clauses in the first query.

If, for some reason, you think that these entirely different-looking queries should yield the same number of rows, then you'll have to tell us how your data is structured.

0

精彩评论

暂无评论...
验证码 换一张
取 消