In this tutorial, you'll learn how to use the case statement in the MySQL database.
—
Facebook: / gokcedbsql
—
Video Transcript:
—
Connect MySQL In Python: • How To Connect To MySQL In Python (2 ...
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn three ways to use the case statement in MySQL database. Let's start by looking at the entity relationship diagram.
I have 15 tables in my database right now but in this tutorial, we'll mostly be working on the property cash underscore sales and terms underscore sale tables. Number one says I wanted to select all the property numbers from the property table and label them as either even or odd. To do that, I'll use the case statement.
Here I'm making use of the when and the then keyword. When property number percent 2 is equal to 0 then return even else return odd, I'm also using the as a keyword to create even or odd for the key statement. Number two let's look at a little bit more complicated example.
Say I wanted to print all the property numbers and label them as either cash sale, term sales, or neither cash nor term sale. To do that, I'll write two sub-queries in the case statement. The first sub-query is checking whether the property id exists in the cash underscore sales table.
If it does then label the row as cash sale similarly the second sub-query checks whether the property id exists in the terms underscore sales table. If it does then label the row as terms sale and if the property id doesn't exist in either the cash sales table then label the row as neither terms nor cash sales. I'm also using the ask keyword to create a cache or term sale alias for the case statement.
Number three, you can also use the case statement in the order by clause. Here I'm selecting the property number, property state, and property subdivision from the property table. In the order by clause, I'm saying if the property subdivision is null then order by property state else order by property subdivision.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.
1. SELECT property_number, (CASE WHEN property_number % 2=0 THEN 'EVEN' ELSE 'ODD' END) AS 'EVEN OR ODD' FROM property;
2. SELECT property.property_number, (CASE WHEN property.id = (SELECT cash_sales.property_id FROM cash_sales WHERE cash_sales.property_id = property.id) THEN 'CASH SALE' WHEN property.id = (SELECT terms_sales.property_id FROM terms_sales WHERE terms_sales.property_id = property.id) THEN 'TERMS SALE' ELSE 'NEITHER TERMS NOR CASH SALE' END) AS 'CASH OR TERMS SALE' FROM property;
3. SELECT property_number, property_state, property_subdivision FROM property ORDER BY(CASE WHEN property_subdivision IS NULL THEN property_state ELSE property_subdivision END);
Watch video 3 Ways To Use Case Statement (2 Min) In MySQL online, duration hours minute second in high quality that is uploaded to the channel Gokce DB 19 March 2022. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 209 times and liked it 5 visitors.