检查 joined table 中的记录是否超过一条记录
Check if records are more than one for a record in joined table
我有两个 table:business
和 sms_content_business
这是两个 table 的架构:
对于 business
Table:
对于 sms_content_business
Table:
现在我想要实现的是获取 is_topbrand = 1
在 sms_content_business
table 中至少有一条记录的所有业务,我这样做是这样的:
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
class 来做到这一点?有帮助吗?
对于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
)
我有两个 table:business
和 sms_content_business
这是两个 table 的架构:
对于 business
Table:
对于 sms_content_business
Table:
现在我想要实现的是获取 is_topbrand = 1
在 sms_content_business
table 中至少有一条记录的所有业务,我这样做是这样的:
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
class 来做到这一点?有帮助吗?
对于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
)