Go Back   CodingForums.com > :: Server side development > MySQL > Other Databases

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2007, 07:46 AM   PM User | #1
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
disappearing records ???

Hi,

I have this problem i cannot explain why it is happening. So perhaps one of you can.

I made a query to return records from a table with a few joins to get extra information. This query returns 151597 records.

I have put this query in a view so it can be accessed easy using excel for creating pivottables. The view returns 151597 records. All is fine.

Now i have added a query to another table pulling more records, combining them using UNION keyword. The first recordset is identified with a statusfield with a certain value, the second recordset has another value for this statusfield.

When i run this view and use a filter to get all the records from the first recordset, it suddenly returns 150086 records. 1511 less

so i did this in query analyser:
Code:
select count(*) from
(
   select 'shipped' as status, someotherfields
   from table1
) as subselect
where status = 'shipped'
returned the correct number of records (as much records as there are in table1)

Code:
select count(*) from
(
   select 'shipped' as status, someotherfields
   from table1
   union
   select 'contracted' as status, someotherfields
   from table2
) as subselect
where status = 'shipped'
returned the wrong number of records (less than previous)

how is that possible????? adding records with status 'contracted' will reduce the number of records with status 'shipped'???

really confused here
Roelf is offline   Reply With Quote
Old 09-20-2007, 01:19 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
A UNION removes duplicates

say you have values A, B, C, D in table one and values A and D in table two.

a UNION returns only 4 rows. If you want all values for A and D returned you need a UNION ALL which returns A, A, B, C, D, D
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
Roelf (09-20-2007)
Old 09-20-2007, 01:57 PM   PM User | #3
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
ofcourse, i knew the difference between union and union all

but i did not realize the practical implications until now

Thanks a lot!
Roelf is offline   Reply With Quote
Old 09-22-2007, 01:31 AM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
no worries, sometimes it takes a different set of eyes to tell you what you already know.
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.