...

View Full Version : mysql IF statements



mhunt
01-23-2007, 08:42 PM
I looked through the mysql manual but their examples are pretty confusing
anyone think they could show me the correct way to write an IF statement inside of a query like this one?



SELECT orders.order_id, students.fname, students.lname, order_status.order_status_name, course.name AS course_name, date_format(class.startdate, '%m/%d/%Y') AS startdate, date_format(class.enddate, '%m/%d/%Y') as enddate, order_type.order_type FROM orders LEFT JOIN students ON orders.student_id = students.student_id LEFT JOIN order_status ON orders.order_status = order_status.order_status_id LEFT JOIN class ON orders.class_id = class.class_id LEFT JOIN course ON orders.course_id = course.course_id LEFT JOIN order_type ON orders.order_type = order_type.type_id IF(order_type.type_id == 1) THEN SELECT lm_number FROM lmcharge WHERE orders.order_id = lmcharge.order_id; END IF WHERE orders.order_id = '8'

Fumigator
01-23-2007, 11:52 PM
You need to put your selections (including the IF statement) before your "from" clause.



SELECT orders.order_id
,students.fname
, students.lname
, order_status.order_status_name
, course.name AS course_name
, date_format(class.startdate, '%m/%d/%Y') AS startdate
, date_format(class.enddate, '%m/%d/%Y') as enddate
, order_type.order_type
, IF(order_type.type_id == 1, lmcharge.lm_number, 0)
FROM orders
LEFT JOIN students ON orders.student_id = students.student_id
LEFT JOIN order_status ON orders.order_status = order_status.order_status_id
LEFT JOIN class ON orders.class_id = class.class_id
LEFT JOIN course ON orders.course_id = course.course_id
LEFT JOIN order_type ON orders.order_type = order_type.type_id
LEFT JOIN lmcharge ON orders.order_id = lmcharge.order_id
WHERE orders.order_id = '8'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum