You get errors because the CREATE FUNCTION syntax is incorrect (you must love these MySQL user comments manually!). The correct syntax for creating this function is as follows:
CREATE FUNCTION book_subject() RETURNS VARCHAR(64) RETURN @subject;
The CREATE VIEW syntax is correct.
To use the view, you need to set the @book_subject variable before choosing from the view:
SET @book_subject = 'Epic Poems';
Then when you do:
SELECT * FROM thematical_books;
He will return the title and author of all books devoted to "Epic verses"
This is a trick to get around the limitation of MySQL views: "SELECT [views] cannot reference system or user variables." You use a function that simply returns a variable, and that function is called every time the view is used.
source share