MySql where clause conditional field value

I need to select records using a condition added only if a field value exists. My query uses a left join between table A and B, and table B may not have a record for every record in table A, so fields selected from B may have null values. If the value for field B.1 exists so is not null then I want to add a condition to the where clause. Is this possible? I tried the following:
 select A.field1, B.field2 from A, B where A.field1='somevalue' and (select case when B.status=1 then  B.customers_group_id=2 else 1=1)

Open in new window

But it gave a check syntax error in PhpMyAdmin.
LVL 1
ncwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
I hope that isn't the complete query, you have a Cartesian product being formed between tables A and B.

Can you provide some sample data from tables a and b perhaps?

using real tables names/fields names is fine (in fact better)
0
PortletPaulfreelancerCommented:
you could try this in the meantime, using ANSI join syntax makes this easier
select A.field1, B.field2 
 
FROM A
LEFT JOIN B ON A.SOMETHING = B.SOMETHING --<<< 
           AND  B.status = 1 and  B.customers_group_id = 2 
 where A.field1='somevalue' 
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ncwAuthor Commented:
I think you've answered it, the 'and  B.customers_group_id = 2' needs to go in the join sql, which I completely missed including in my example code.
0
PortletPaulfreelancerCommented:
:) I thought something was missing... happy you got the solution. Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.