PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] Check if records are more than one for a record in joined table

 March 19, 2022     database, mysql, yii, yii1.x     No comments   

Issue

I have two tables: business and sms_content_business And this is the schema for both tables: For business Table: enter image description here

For sms_content_business Table: enter image description here

Now what I want to achieve is to get all the businesses for which is_topbrand = 1 there is at least one record in the sms_content_business table and I am doing it like this:

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

But it doesn't do what I intend to do. Also how to do it using Yii 1 CDBcriteria class? Any help?


Solution

For mysql query, you can use 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
)


Answered By - Blank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing