某种连接声明?

时间:2011-02-23 10:35:06

标签: mysql

如何将这两个查询放入单个查询中?

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字段。我想确定是否有任何有效的包。有效包必须处于活动状态且至少有1个活动选项。为此运行2个查询似乎不对。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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