2011-08-01 28 views
0

我編寫一個查詢從3個表中獲取值獲取多個值,但是這是返回多個值,所以任何一個可以告訴我哪裏錯了從查詢

select c.CompanyName,cd.FedTaxID,cd.EmailAddress,cd.PhoneNumber 
from tblcustomerdetail cd,tblcustomer c 
where c.FedTaxID in (
     select FedTaxID 
     from tblcustomer 
     where CustomerID in (
      select LOginID 
      from tbluserlogindetail 
      where UserName like "pa%" and RoleTypeID='20' 
         ) 
        ) 
and cd.FedTaxID in (
     select FedTaxID 
     from tblcustomer 
     where CustomerID in (
      select LOginID 
      from tbluserlogindetail 
      where UserName like "pa%" and RoleTypeID='20' 
         ) 
        ); 

我的關係在這裏

我3個表是`tbluserlogindetails,tblcustomerdetails和tblCustomer」

1) Initially i will get `Login ID` from `tblUserLoginDetail ` based on the `user name`. 

2) Next based on `LoginID` i will get `FedTaxID` from tblcustomerDetail` 

3) Next based on 'FedTaxID' i will get the the required details from `tblcustomer' 
+5

好像過度使用子查詢。 – Jacob

+0

顯示一些數據,tblcustomerdetail和tblcustomer兩個表之間是否有任何關係? – Kangkan

回答

1
SELECT 
    tblcustomer.CompanyName, 
    tblcustomerdetail.FedTaxID, 
    tblcustomerdetail.EmailAddress, 
    tblcustomerdetail.PhoneNumber 

FROM tbluserlogindetail, tblcustomer, tblcustomerdetail 
WHERE 
    tbluserlogindetail.LOginID = tblcustomer.CustomerID 
    AND tblcustomer.FedTaxID = tblcustomerdetail.FedTaxID 

    AND tbluserlogindetail.UserName LIKE 'pa%' 
    AND tbluserlogindetail.RoleTypeID = '20' 

嘗試這樣的事情。

子查詢的性能很慢。

MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?