2012-10-15 24 views
1

以下查詢是我以前使用的查詢...但我想結合這兩個查詢以改進性能如何從兩個不同的表中獲取兩個不同的列值inturn一個值表依賴於其他表值

select a, b, c 
    from table1 
where d LIKE 'xxx' 
    and f like 'yyyy' 
order by b desc; 

我正在執行上述查詢和讀取值。

對於從上面查詢b的每個值,再次在循環中執行下面的查詢。

select count(*) 
    from table2 where b=? AND js_email_id IN 
(
    select js_email_id 
    from js_initialsignup 
    where UCase(jsaccountstatus) LIKE UCase('live') 
    AND UCase(js_status) LIKEUCase('accepted') 
) 

如何組合兩個查詢並一次獲取計數和值?

回答

0
select a,b,c, 
     (select count(*) 
from table2 where b=a.b AND js_email_id IN 
(
    select js_email_id 
    from js_initialsignup 
    where UCase(jsaccountstatus) LIKE UCase('live') 
    AND UCase(js_status) LIKEUCase('accepted') 
)) as cnt 

from table1 a 
+0

這個查詢工作,我感謝.... – mvinay

+0

如果有用,你能接受的答案 – AnandPhadke

+0

是有可能得到從上面的查詢ROW_NUMBERS()在MySQL – mvinay

0

試試這個:

SELECT t1.a, t1.b, t1.c, COUNT(t2.*) 
FROM table1 t1 
INNER JOIN table2 t2 ON t1.b = t2.b 
INNER JOIN js_initialsignup j ON t2.js_email_id = j.js_email_id 
WHERE t1.d LIKE 'xxx' 
    AND t1.f like 'yyyy' 
    AND UCase(j.jsaccountstatus) LIKE UCase('live') 
    AND UCase(j.js_status) LIKE UCase('accepted'))" 
GROUP BY t1.a, t1.b, t1.c 
ORDER BY by t1.b DESC; 
相關問題