2017-05-08 71 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