2011-02-23 53 views
0

如何將這兩個查詢放入單個查詢中?某種聯接語句?

select count(id) as cnt from {$site_id}.proofingv2_packages where active='1' 
select count(id) as cnt from {$site_id}.proofingv2_package_options where active='1' and parent={$row["id"]} order by sort 

$row['id']是來自第一個查詢的id字段。我試圖確定是否有任何有效的軟件包。有效的軟件包必須處於活動狀態,並且至少有一個活動選項。爲此運行2個查詢似乎不正確。

任何人都可以幫忙嗎?

回答

0
select count(id) as cnt from 
{$site_id}.proofingv2_packages pp 
INNER JOIN 
{$site_id}.proofingv2_package_options 
pt ON pp.active = pt.Active AND 
pp.Active = 1 

如果id爲這兩個表上相同的PK或FK使用此查詢

select count(id) as cnt from 
    {$site_id}.proofingv2_packages pp 
    INNER JOIN {$site_id}.proofingv2_package_options pt ON pp.id= pt.id 
    AND pp.Active = 1 
+0

謝謝。更新爲:'select count(id)as cnt from {$ site_id} .proofingv2_packages pp INNER JOIN {$ site_id} .proofingv2_package_options pt ON pp.id = pt.parent WHERE pp.active = 1 AND pt.active = 1' – John 2011-02-23 10:54:05

0
SELECT IF(count(*) > 0, 1, 0) AS isValid 
FROM {$site_id}.proofingv2_packages pp 
INNER JOIN {$site_id}.proofingv2_package_options ppo ON ppo.parent = pp.id 
WHERE pp.active = '1' 
AND ppo.active = '1' 

這應該返回1,如果有有效的包或0,如果沒有