troubleshooting Question

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

Avatar of MFredin
MFredinFlag for United States of America asked on
MySQL ServerColdFusion Language
2 Comments1 Solution524 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros