Det SAG er en sætning, der driver hvis-så-andet-typen af logiske forespørgsler. Denne sætning returnerer værdien, når den angivne betingelse evalueres til Sand. Når ingen betingelse evalueres til Sand, returnerer den værdien af ELSE-delen.
Når der ikke er nogen ELSE-del, og ingen betingelse evalueres til Sand, returnerer den en NULL-værdi.
I Structured Query Language bruges CASE-sætningen i SELECT-, INSERT- og DELETE-sætninger med følgende tre sætninger:
- HVOR klausul
- BESTIL EFTER Klausul
- GRUPPE EFTER klausul
Denne sætning i SQL efterfølges altid af mindst et par WHEN- og THEN-sætninger og afsluttes altid med nøgleordet END.
CASE-sætningen er af to typer i relationelle databaser:
hacking behandling
- Simpel CASE-erklæring
- Søgte CASE-udsagn
Syntaks for CASE-sætning i SQL
CASE WHEN condition_1 THEN statement_1 WHEN condition_2 THEN statement_2 ……. WHEN condition_N THEN statement_N ELSE result END;
Her evaluerer CASE-sætningen hver tilstand en efter en.
Hvis udtrykket matcher betingelsen i den første WHEN-sætning, springer det alle de yderligere WHEN- og THEN-betingelser over og returnerer sætningen_1 i resultatet.
Hvis udtrykket ikke matcher den første WHEN-betingelse, sammenlignes det med den sekundære WHEN-betingelse. Denne matchningsproces fortsætter, indtil udtrykket matches med en hvilken som helst WHEN-betingelse.
Hvis ingen betingelse matches med udtrykket, går kontrollen automatisk til ELSE-delen og returnerer sit resultat. I CASE-syntaksen er ELSE-delen valgfri.
sammenlignelig java
I syntaks er CASE og END de vigtigste nøgleord, som viser begyndelsen og slutningen af CASE-sætningen.
Eksempler på CASE-sætning i SQL
Lad os tage tabellen Student_Details, som indeholder roll_no, navn, karakterer, emne og by for elever.
string builder java
Rulle_Nr | Stu_navn | Stu_Subject | Stu_Marks | Stu_City |
---|---|---|---|---|
2001 | Akshay | Videnskab | 92 | Noida |
2002 | Vædder | Matematik | 49 | Jaipur |
2004 | Shyam | engelsk | 52 | Gurgaon |
2005 | yatin | Nix | Fire. Fem | Lucknow |
2006 | Manoj | Computer | 70 | Ghaziabad |
2007 | Plader | Matematik | 82 | Noida |
2008 | Håret | Videnskab | 62 | Gurgaon |
2009 | Yogesh | engelsk | 42 | Lucknow |
2010 | Vædder | Computer | 88 | Delhi |
2011 | Shyam | Nix | 35 | Kanpur |
Eksempel 1: Følgende SQL-sætning bruger en enkelt WHEN- og THEN-betingelse til CASE-sætningen:
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;
Forklaring af ovenstående forespørgsel:
Her tjekker CASE-redegørelsen, at hvis Stu_Marks er større end og lig med 50, returnerer den Studerende_Bestået ellers flytter til ANDET del og returnerer Student_Failed i Elev_Resultat kolonne.
Produktion:
Rulle_Nr | Stu_navn | Stu_Subject | Stu_Marks | Elev_Resultat |
---|---|---|---|---|
2001 | Akshay | Videnskab | 92 | Studerende_Bestået |
2002 | Vædder | Matematik | 49 | Student_Failed |
2004 | Shyam | engelsk | 52 | Studerende_Bestået |
2005 | yatin | Nix | Fire. Fem | Student_Failed |
2006 | Manoj | Computer | 70 | Studerende_Bestået |
2007 | Plader | Matematik | 82 | Studerende_Bestået |
2008 | Håret | Videnskab | 62 | Studerende_Bestået |
2009 | Yogesh | engelsk | 42 | Student_Failed |
2010 | Vædder | Computer | 88 | Studerende_Bestået |
2011 | Shyam | Nix | 35 | Student_Failed |
Eksempel 2: Følgende SQL-sætning tilføjer mere end én WHEN- og THEN-betingelse til CASE-sætningen:
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>
Produktion:
Emp_Id | Emp_Name | Emp_Dept | Samlet_Løn | Emp_Bemærkninger |
---|---|---|---|---|
1 | Akshay | Finansiere | 17.000 | Forøgelse |
2 | Vædder | Markedsføring | 9000 | Nedsættelse |
3 | Shyam | Salg | 10.000 | Forøgelse |
4 | yatin | Kodning | 12000 | Forøgelse |
5 | Manoj | Markedsføring | 8000 | Nedsættelse |
Eksempel 4: I dette eksempel bruger vi ORDER BY-sætningen med en CASE-sætning i SQL:
rekursion java
Lad os tage en anden Employee_Details-tabel, som indeholder Emp_ID, Emp_Name, Emp_Dept og Emp_Age.
Vi kan kontrollere dataene for Employee_Details ved at bruge følgende forespørgsel i SQL:
Select * From Employee_Details;
Produktion:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
1 | Akshay | Finansiere | 23 |
2 | Vædder | Markedsføring | 24 |
3 | Balram | Salg | 25 |
4 | yatin | Kodning | 22 |
5 | Manoj | Markedsføring | 23 |
6 | Plader | Finansiere | 24 |
7 | Håret | Finansiere | 22 |
8 | Yogesh | Kodning | 25 |
9 | Naveen | Markedsføring | 22 |
10 | Tarun | Finansiere | 23 |
Følgende SQL-forespørgsel viser alle oplysninger om medarbejdere i stigende rækkefølge af medarbejdernavne:
SELECT * FROM Employee_Details ORDER BY Emp_Name;
Produktion:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
1 | Akshay | Finansiere | 23 |
3 | Balram | Salg | 25 |
5 | Manoj | Markedsføring | 23 |
9 | Naveen | Markedsføring | 22 |
7 | Håret | Finansiere | 22 |
2 | Vædder | Markedsføring | 24 |
6 | Plader | Finansiere | 24 |
10 | Tarun | Finansiere | 23 |
4 | yatin | Kodning | 22 |
8 | Yogesh | Kodning | 25 |
Hvis du vil vise de medarbejdere øverst, som arbejder i kodningsafdelingen, skal du til denne operation bruge en enkelt WHEN- og THEN-sætning i CASE-sætningen som vist i følgende forespørgsel:
katodestrålerørsmonitor
SELECT * FROM Employee_Details ORDER BY CASE WHEN Emp_Dept = 'Coding' THEN 0 ELSE 1 END, Emp_Name;
Produktion:
Emp_Id | Emp_Name | Emp_Dept | Emp_Age |
---|---|---|---|
4 | yatin | Kodning | 22 |
8 | Yogesh | Kodning | 25 |
1 | Akshay | Finansiere | 23 |
3 | Balram | Salg | 25 |
5 | Manoj | Markedsføring | 23 |
9 | Naveen | Markedsføring | 22 |
7 | Håret | Finansiere | 22 |
2 | Vædder | Markedsføring | 24 |
6 | Plader | Finansiere | 24 |
10 | Tarun | Finansiere | 23 |
60>