?
Solved

Multiple conditions and values in MySQL query

Posted on 2012-09-19
2
Medium Priority
?
511 Views
Last Modified: 2012-09-19
The following query works fine

SELECT
SR_STU_ENROLL.NAME_L,
SR_STU_ENROLL.NAME_F,
SR_STU_ENROLL.CAMPUS_ID,
IF( SR_STU_ENROLL.GRD_LVL='12', '2013', '' ) AS gradYear,
SR_STU_ENROLL.STU_ID
FROM
SR_STU_ENROLL

How can I modify the if statement so that the grades and gradYears are all covered such as

if grd_lvl = 12 then output 2013
elseif grd_lvel =11 then output 2014

etc down to Kindergarten

Thank you!
0
Comment
Question by:smetterd
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 38415404
USE CASE

SELECT
SR_STU_ENROLL.NAME_L,
SR_STU_ENROLL.NAME_F,
SR_STU_ENROLL.CAMPUS_ID,
CASE SR_STU_ENROLL.GRD_LVL
WHEN '12' then '2013'
WHEN '11' then '2014'
... and so on...
ELSE '' END AS gradYear,
SR_STU_ENROLL.STU_ID
FROM
SR_STU_ENROLL

Open in new window

http://dev.mysql.com/doc/refman/5.0/en/case.html
0
 
LVL 2

Author Closing Comment

by:smetterd
ID: 38415445
Thank you!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question