The CASE je príkaz, ktorý prevádzkuje logické dotazy typu if-then-else. Tento príkaz vráti hodnotu, keď sa špecifikovaná podmienka vyhodnotí ako True. Keď sa žiadna podmienka nevyhodnotí ako True, vráti hodnotu časti ELSE.
string.replaceall v jazyku Java
Ak neexistuje žiadna ELSE časť a žiadna podmienka sa nevyhodnotí ako True, vráti hodnotu NULL.
V Structured Query Language sa príkaz CASE používa v príkazoch SELECT, INSERT a DELETE s nasledujúcimi tromi klauzulami:
- Klauzula WHERE
- OBJEDNAŤ PODĽA DOLOŽKY
- GROUP BY klauzule
Po tomto príkaze v SQL vždy nasleduje aspoň jeden pár príkazov WHEN a THEN a vždy sa končí kľúčovým slovom END.
Príkaz CASE je v relačných databázach dvoch typov:
- Jednoduchý príkaz CASE
- Hľadaný príkaz CASE
Syntax príkazu CASE v SQL
CASE WHEN condition_1 THEN statement_1 WHEN condition_2 THEN statement_2 ……. WHEN condition_N THEN statement_N ELSE result END;
Tu príkaz CASE vyhodnotí každú podmienku jednu po druhej.
Ak sa výraz zhoduje s podmienkou prvej klauzuly WHEN, preskočí všetky ďalšie podmienky WHEN a THEN a vo výsledku vráti príkaz_1.
Ak sa výraz nezhoduje s prvou podmienkou WHEN, porovná sa so sekundovou podmienkou WHEN. Tento proces zhody bude pokračovať, kým sa výraz nezhoduje s akoukoľvek podmienkou WHEN.
Ak výrazu nevyhovuje žiadna podmienka, ovládací prvok automaticky prejde do časti ELSE a vráti svoj výsledok. V syntaxi CASE je časť ELSE voliteľná.
V syntaxi sú CASE a END najdôležitejšie kľúčové slová, ktoré zobrazujú začiatok a koniec príkazu CASE.
c kód abs
Príklady príkazu CASE v SQL
Vezmime si tabuľku Student_Details, ktorá obsahuje číslo role, meno, známky, predmet a mesto študentov.
Roll_No | Stu_Name | Stu_Subject | Stu_Marks | Stu_City |
---|---|---|---|---|
2001 | Akshay | Veda | 92 | Noida |
2002 | Ram | Matematika | 49 | Jaipur |
2004 | Shyam | Angličtina | 52 | Gurgaon |
2005 | yatin | nie | Štyri | Lucknow |
2006 | Manoj | Počítač | 70 | Ghaziabad |
2007 | Plechové | Matematika | 82 | Noida |
2008 | Vlasy | Veda | 62 | Gurgaon |
2009 | Yogesh | Angličtina | 42 | Lucknow |
2010 | Ram | Počítač | 88 | Dillí |
2011 | Shyam | nie | 35 | Kanpur |
Príklad 1: Nasledujúci príkaz SQL používa jednu podmienku WHEN a THEN pre príkaz CASE:
SELECT Roll_No, Stu_Name, Stu_Subject, Stu_marks, CASE WHEN Stu_Marks >= 50 THEN 'Student_Passed' ELSE 'Student_Failed' END AS Student_Result FROM Student_Details;
Vysvetlenie vyššie uvedeného dotazu:
Tu príkaz CASE skontroluje, že ak Stu_Marks je väčšia a rovná sa 50, vráti Študent_prešiel inak sa presúva do ELSE časť a vrátenie Student_Failed v Študent_Výsledok stĺpec.
triedenie zoznamu polí java
Výkon:
Roll_No | Stu_Name | Stu_Subject | Stu_Marks | Študent_Výsledok |
---|---|---|---|---|
2001 | Akshay | Veda | 92 | Študent_prešiel |
2002 | Ram | Matematika | 49 | Student_Failed |
2004 | Shyam | Angličtina | 52 | Študent_prešiel |
2005 | yatin | nie | Štyri | Student_Failed |
2006 | Manoj | Počítač | 70 | Študent_prešiel |
2007 | Plechové | Matematika | 82 | Študent_prešiel |
2008 | Vlasy | Veda | 62 | Študent_prešiel |
2009 | Yogesh | Angličtina | 42 | Student_Failed |
2010 | Ram | Počítač | 88 | Študent_prešiel |
2011 | Shyam | nie | 35 | Student_Failed |
Príklad 2: Nasledujúci príkaz SQL pridáva do príkazu CASE viac ako jednu podmienku WHEN a THEN:
SELECT Roll_No, Stu_Name, Stu_Subject, Stu_marks, CASE WHEN Stu_Marks >= 90 THEN 'Outstanding' WHEN Stu_Marks >= 80 AND Stu_Marks = 70 AND Stu_Marks = 60 AND Stu_Marks = 50 AND Stu_Marks <60 50 then 'bad' when stu_marks < 'failed' end as stu_remarks from student_details; pre> <p> <strong>Explanation of above query:</strong> </p> <p>Here, the CASE statement checks multiple WHEN and THEN conditions one by one. If the value of <strong>Stu_Marks</strong> column is greater than or equals to <strong>90</strong> , it returns <strong>Outstanding</strong> otherwise moves to the further WHEN and THEN conditions.</p> <p>If none of the conditions is matched with the <strong>Student_Details</strong> table, CASE returns <strong>the NULL</strong> value in the <strong>Stu_Remarks</strong> column because there is no ELSE part in the query.</p> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Roll_No</th> <th>Stu_Name</th> <th>Stu_Subject</th> <th>Stu_Marks</th> <th>Stu_Remarks</th> </tr> <tr> <td>2001</td> <td>Akshay</td> <td>Science</td> <td>92</td> <td>Outstanding</td> </tr> <tr> <td>2002</td> <td>Ram Math</td> <td>49</td> <td>Failed</td> </tr> <tr> <td>2004</td> <td>Shyam</td> <td>English</td> <td>52</td> <td>Bad</td> </tr> <tr> <td>2005</td> <td>Yatin</td> <td>Hindi</td> <td>45</td> <td>Failed</td> </tr> <tr> <td>2006</td> <td>Manoj</td> <td>Computer</td> <td>70</td> <td>Good</td> </tr> <tr> <td>2007</td> <td>Sheetal</td> <td>Math</td> <td>82</td> <td>Excellent</td> </tr> <tr> <td>2008</td> <td>Parul</td> <td>Science</td> <td>62</td> <td>Average</td> </tr> <tr> <td>2009</td> <td>Yogesh</td> <td>English</td> <td>42</td> <td>Failed</td> </tr> <tr> <td>2010</td> <td>Ram</td> <td>Computer</td> <td>88</td> <td>Excellent</td> </tr> <tr> <td>2011</td> <td>Shyam</td> <td>Hindi</td> <td>35</td> <td>Failed</td> </tr> </table> <p> <strong>Example 3:</strong> </p> <p>Let's take another Employee_Details table which contains Emp_ID, Emp_Name, Emp_Dept, and Emp_Salary.</p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Salary</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>9000</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>4000</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Sales</td> <td>5000</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>4000</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>5000</td> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>8000</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Coding</td> <td>6000</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Coding</td> <td>4000</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Marketing</td> <td>8000</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Finance</td> <td>3000</td> </tr> </table> <p> <strong>The following SQL query uses GROUP BY clause with CASE statement:</strong> </p> <pre> SELECT Emp_Id, Emp_Name, Emp_Dept, sum(Emp_Salary) as Total_Salary, CASE WHEN SUM(Emp_Salary) >= 10000 THEN 'Increment' ELSE 'Constant' END AS Emp_Remarks FROM Employee_Details GROUP BY Emp_id, Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Total_Salary</th> <th>Emp_Remarks</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>17000</td> <td>Increment</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>9000</td> <td>Decrement</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Sales</td> <td>10000</td> <td>Increment</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>12000</td> <td>Increment</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>8000</td> <td>Decrement</td> </tr> </table> <p> <strong>Example 4: In this example, we use the ORDER BY clause with a CASE statement in SQL:</strong> </p> <p>Let's take another Employee_Details table which contains Emp_ID, Emp_Name, Emp_Dept, and Emp_Age.</p> <p>We can check the data of Employee_Details by using the following query in SQL:</p> <pre> Select * From Employee_Details; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> </table> <p>The following SQL query shows all the details of employees in the ascending order of employee names:</p> <pre> SELECT * FROM Employee_Details ORDER BY Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> </table> <p>If you want to show those employees at the top who work in the Coding Department, then for this operation, you have to use single WHEN and THEN statement in the CASE statement as shown in the following query:</p> <pre> SELECT * FROM Employee_Details ORDER BY CASE WHEN Emp_Dept = 'Coding' THEN 0 ELSE 1 END, Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> </table> <hr></60>
Výkon:
Emp_Id | Emp_Name | Emp_Dept | Celková_Mzda | Emp_Remarks |
---|---|---|---|---|
1 | Akshay | Financie | 17 000 | Prírastok |
2 | Ram | marketing | 9000 | Dekrementovať |
3 | Shyam | Predaj | 10 000 | Prírastok |
4 | yatin | Kódovanie | 12 000 | Prírastok |
5 | Manoj | marketing | 8000 | Dekrementovať |
Príklad 4: V tomto príklade používame klauzulu ORDER BY s príkazom CASE v SQL:
Zoberme si ďalšiu tabuľku Employee_Details, ktorá obsahuje Emp_ID, Emp_Name, Emp_Dept a Emp_Age.
Údaje Employee_Details môžeme skontrolovať pomocou nasledujúceho dotazu v SQL:
Select * From Employee_Details;
Výkon:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
1 | Akshay | Financie | 23 |
2 | Ram | marketing | 24 |
3 | Balram | Predaj | 25 |
4 | yatin | Kódovanie | 22 |
5 | Manoj | marketing | 23 |
6 | Plechové | Financie | 24 |
7 | Vlasy | Financie | 22 |
8 | Yogesh | Kódovanie | 25 |
9 | Naveen | marketing | 22 |
10 | Tarun | Financie | 23 |
Nasledujúci SQL dotaz zobrazuje všetky podrobnosti o zamestnancoch vo vzostupnom poradí mien zamestnancov:
SELECT * FROM Employee_Details ORDER BY Emp_Name;
Výkon:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
1 | Akshay | Financie | 23 |
3 | Balram | Predaj | 25 |
5 | Manoj | marketing | 23 |
9 | Naveen | marketing | 22 |
7 | Vlasy | Financie | 22 |
2 | Ram | marketing | 24 |
6 | Plechové | Financie | 24 |
10 | Tarun | Financie | 23 |
4 | yatin | Kódovanie | 22 |
8 | Yogesh | Kódovanie | 25 |
Ak chcete zobraziť tých zamestnancov na vrchole, ktorí pracujú v oddelení kódovania, potom pre túto operáciu musíte použiť jeden príkaz WHEN a THEN v príkaze CASE, ako je uvedené v nasledujúcom dotaze:
regresné testovanie v testovaní softvéru
SELECT * FROM Employee_Details ORDER BY CASE WHEN Emp_Dept = 'Coding' THEN 0 ELSE 1 END, Emp_Name;
Výkon:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
4 | yatin | Kódovanie | 22 |
8 | Yogesh | Kódovanie | 25 |
1 | Akshay | Financie | 23 |
3 | Balram | Predaj | 25 |
5 | Manoj | marketing | 23 |
9 | Naveen | marketing | 22 |
7 | Vlasy | Financie | 22 |
2 | Ram | marketing | 24 |
6 | Plechové | Financie | 24 |
10 | Tarun | Financie | 23 |
60>