Skip to Content
MySQL 8 Cookbook
book

MySQL 8 Cookbook

by Karthik Appigatla
January 2018
Intermediate to advanced
446 pages
12h 57m
English
Packt Publishing
Content preview from MySQL 8 Cookbook

How to do it...

Here is an example of how to write a function and how to call it. Suppose a banker wants to give a credit card based on income level, instead of exposing the actual salary, you can expose this function to find out the income level:

shell> vi function.sql;DROP FUNCTION IF EXISTS get_sal_level;DELIMITER $$CREATE FUNCTION get_sal_level(emp int) RETURNS VARCHAR(10) DETERMINISTICBEGIN DECLARE sal_level varchar(10); DECLARE avg_sal FLOAT; SELECT AVG(salary) INTO avg_sal FROM salaries WHERE emp_no=emp;  IF avg_sal < 50000 THEN SET sal_level = 'BRONZE'; ELSEIF (avg_sal >= 50000 AND avg_sal < 70000) THEN SET sal_level = 'SILVER'; ELSEIF (avg_sal >= 70000 AND avg_sal < 90000) THEN SET sal_level = 'GOLD'; ELSEIF (avg_sal >= 90000) THEN ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

MySQL Cookbook

MySQL Cookbook

Paul DuBois
Advanced MySQL 8

Advanced MySQL 8

Eric Vanier, Birju Shah, Tejaswi Malepati

Publisher Resources

ISBN: 9781788395809Other