Avatar of MFredin
MFredinFlag for United States of America asked on

Can I use IF THEN statements in MySQL? Please help with query

I'm using MySQL 5 and Coldfusion.  Is there any way I can use an IF THEN ELSE statement in my SQL to determine which field gets used in my WHERE statement?  If not, is there another way I can do this?

SELECT SUM(head_count) as head_count, AVG(weight) as avg_weight, AVG(purchase_price) as avg_price, type
FROM deal_master LEFT JOIN deal_lots ON deal_lots.deal_master_id = deal_master.deal_master_id
WHERE delivered_date IS NULL
AND sale_id IS NULL
AND type = 'S'
AND type_2 = 'Y'

IF delivery_date IS NOT NULL
AND MONTH(delivery_date) = '5'
ELSE
AND MONTH(delivery_date_from) = '5'
END

GROUP BY type
IF delivery_date IS NOT NULL
AND MONTH(delivery_date) = '5'
ELSE
AND MONTH(delivery_date_from) = '5'
END

Open in new window

MySQL ServerColdFusion Language

Avatar of undefined
Last Comment
MFredin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
MFredin

THANKS!
Your help has saved me hundreds of hours of internet surfing.
fblack61