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 > Left Join two tables---so slow!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-09, 17:35
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Left Join two tables---so slow!

I'm trying to find all members in the CBA table who have a record in either the DUES_PAID table with 2008 in the DUES_Year column or who have 2009 in the itemname column of paypal_payment_info.

This works but takes about two minutes! Any suggestions for speeding this up?

Nick

SELECT c.CBA_ID, c.LNAME, c.FN_MI, p.itemnumber, d.DUES_YEAR
FROM ((CBA c
LEFT JOIN DUES_PAID d ON d.CBA_ID=c.CBA_ID)
LEFT JOIN paypal_payment_info p ON p.custom=c.CBA_ID)
WHERE d.DUES_YEAR =2008 OR p.itemnumber = 2009

ORDER BY c.LNAME ASC
Reply With Quote
  #2 (permalink)  
Old 01-30-09, 17:56
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
In reality want to see if one member, "3667" from the CBA table has paid his dues in one table or the other.

SELECT c.CBA_ID, c.LNAME, c.FN_MI, p.itemnumber, d.DUES_YEAR
FROM ((CBA c
LEFT JOIN DUES_PAID d ON d.CBA_ID=c.CBA_ID)
LEFT JOIN paypal_payment_info p ON p.custom=c.CBA_ID)
WHERE c.CBA_ID = 3667 AND d.DUES_YEAR =2008 OR p.itemnumber = 2009

ORDER BY c.LNAME ASC
Reply With Quote
  #3 (permalink)  
Old 01-30-09, 22:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
here's another approach...
Code:
SELECT c.CBA_ID
     , c.LNAME
     , c.FN_MI
     , 2008     AS year_paid
  FROM CBA AS c
INNER
  JOIN DUES_PAID AS d 
    ON d.CBA_ID = c.CBA_ID
   AND d.DUES_YEAR = 2008
 WHERE c.CBA_ID = 3667 
UNION ALL
SELECT c.CBA_ID
     , c.LNAME
     , c.FN_MI
     , 2009     AS year_paid
  FROM CBA AS c
INNER
  JOIN paypal_payment_info AS p 
    ON p.custom = c.CBA_ID
   AND p.itemnumber = 2009
 WHERE c.CBA_ID = 3667
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-02-09, 00:25
hkp819 hkp819 is offline
Registered User
 
Join Date: Dec 2008
Posts: 59
these are the online sql learning site here you can take the help
SQL Tutorial
SQL Tutorials - Lesson 1: SQL Startup
__________________
New York Web design
Website design

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