2017-05-08 28 views
0

我有两个表:businesssms_content_business这是两个表的架构: 为business表: enter image description here检查记录是一个用于连接表的记录超过

对于sms_content_business表: enter image description here

现在我要实现的是让所有的企业为其is_topbrand = 1有在sms_content_business表中的至少一个记录,我这样做是这样的:

SELECT * FROM `business` WHERE is_topbrand=1 AND (SELECT COUNT(*) FROM `sms_content_business` scb JOIN `business` b ON b.id=scb.business_id) > 0 

但它没有做我打算做的事情。还怎么用Yii 1 CDBcriteria这个类呢?任何帮助?

回答

1

对于mysql查询,可以使用exists;

select b.* 
from business b 
where b.is_topbrand = 1 
and exists(
    select 1 
    from sms_content_business scb 
    where b.id = scb.business_id 
) 
+0

哦,谢谢哥们,它的工作! – Saani

相关问题