我有两个表:business
和 sms_content_business
这是两个表的架构:
对于 business
表:
现在我想要实现的是获取 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
类来做到这一点?有什么帮助吗?
最佳答案
对于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
)
关于mysql - 检查连接表中的记录是否多于一条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43840926/