mysql - 检查连接表中的记录是否多于一条

标签 mysql database yii yii1.x

我有两个表:businesssms_content_business 这是两个表的架构: 对于 business 表: enter image description here

对于 sms_content_business 表: enter image description here

现在我想要实现的是获取 is_topbrand = 1sms_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/

相关文章:

php - Yii 框架 : Display associated table entry through foreign key and search against it

php - 查找顶级父级(递归)

android - 如何使用网络服务?

mysql - 错误 1215 : Cannot add foreign key constraint - MySQL (Simple Tables)

javascript - 通过ajax发送数组

php - 没有 View 的 Yii Controller Action

python - 在python中将数据从另一个数据库导入到数据库

mysql - 已设置MYSQL_ROOT_PASSWORD,但在Docker容器中获取 “Access denied for user ' root'@'localhost'(使用密码:是)

php - 正则表达式:忽略字符串比较中的字符

javascript - 单击加载 PHP 文件