Syntax:
CASE value
WHEN [compare_value
] THEN result
[WHEN [compare_value
] THEN result
...] [ELSE result
] END
CASE WHEN [condition
] THEN result
[WHEN [condition
] THEN result
...] [ELSE result
] END
Following is the Example for Switch Case query =>
SELECT u.`fname` as 'First Name' , u.`lname` as 'Last Name', u.`email` as 'Email', u.`tel_no` as 'Phone', u.country as 'Country', ug.start_date as 'Start Date', ug.end_date as 'End Date', (
CASE ug.end_date
WHEN '0000-00-00 00:00:00'
THEN 'active'
ELSE IF (
DATEDIFF(
now( ) , ug.end_date
) >0, 'inactive', 'active'
)
END
) AS
Product FROM `users` u, (
SELECT user_id, user_type_id, start_date, end_date, user_types.name
FROM user_groups, user_types
WHERE user_type_id
AND user_groups.user_type_id = user_types.id
GROUP BY user_id
ORDER BY user_type_id DESC
)ug
LEFT JOIN (
SELECT user_id, max( login_time ) AS last_login_date
FROM user_login_log
GROUP BY user_id
) AS ull ON ug.user_id = ull.user_id
WHERE ug.user_id = u.id
Syntax:
mysql>SELECT IF(1>2,2,3);
-> 3
mysql>SELECT IF(1<2,'yes','no');
-> 'yes'
mysql>SELECT IF(STRCMP('test','test1'),'no','yes');
Following is the Example for IF-ELSE query =>
SELECT u.`id` AS 'User_Id', u.`fname` AS 'First_Name', u.`lname` AS 'Last_Name', u.`email` AS 'Email', domains.blog_id, domains.site, domains.status
FROM `users` AS u, `user_system` AS us, (
SELECT wp_blogs.blog_id, wp_blogs.domain AS sub_domain, IF(wp_blogs.public <> 0, 'active', 'inactive') AS status, wp_dm.domain AS site
FROM im500_live_wpmu.`wp_blogs` AS wp_blogs, im500_live_wpmu.`wp_domain_mapping` AS wp_dm
WHERE wp_blogs.blog_id = wp_dm.blog_id
AND wp_blogs.public = '1'
) AS domains
WHERE u.id = us.user_id
AND domains.sub_domain LIKE concat( u.username, '.%' )
AND us.system_id =3
For more details: http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html
No comments:
Post a Comment