If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > union 2 tables wrong result

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-09, 05:14
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
union 2 tables wrong result

hi,
can anyone please tell me my error here.

Code:
( SELECT
  Branch
  ,'Pawnshop' as 'Business Name'
  , DateRedeem AS 'Date'
  , principal * interest AS 'Net Profit'
  , 'Redeem' as 'Type'
FROM pawnshop_tbl WHERE ServiceType = 'Redeem'|| ServiceType = 'Renew' )


UNION

( SELECT
  Branch
  ,'Pawnshop' as 'Business Name'
  , DateSold AS 'Date'
  , AuctionPrice - Principal AS 'Net Profit'
  , 'Sold' as 'Type'
  FROM sold_tbl );
it outputs wrong count of records, 27 records only, and what i want is 60 records.

what i need is joining the results of two tables.

please.

thanks in advance
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;

Last edited by homer.favenir; 01-20-09 at 05:18.
Reply With Quote
  #2 (permalink)  
Old 01-20-09, 05:29
bklr bklr is offline
Registered User
 
Join Date: Dec 2008
Posts: 133
check once there are duplicate records are there in ur tables
keep union all and try that statement
Reply With Quote
  #3 (permalink)  
Old 01-20-09, 05:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
bklr, nice guess, but there cannot possibly be duplicates resulting from the union

reason: all the rows from the first SELECT have 'Redeem' as 'Type', while all the rows from the second SELECT have 'Sold' as 'Type'

nevertheless, there could conceivably be plenty of dupes originating in each SELECT separately

you're right about UNION ALL

homer, use UNION ALL instead of UNION and see what happens
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 01-20-09, 06:01
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
Thanks! it now gives me correct results.
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #5 (permalink)  
Old 01-21-09, 01:28
bklr bklr is offline
Registered User
 
Join Date: Dec 2008
Posts: 133
Quote:
Originally Posted by homer.favenir
Thanks! it now gives me correct results.
ur welcome
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On