MySQL supports views, so you can save a query as a view, and use that view like you would a subselect. This is really very simple. For this example, we'll use the database. And here's a useful little query that gets the as minutes and seconds I can say SELECT ID, album_id, title, track_number. And duration DIV 60 AS minutes.
And duration MOD 60 as seconds FROM track. And when I run this, you'll see we have the entire track table. And instead of the duration as a number of seconds we've got it in minutes and seconds. And that's actually pretty useful. Now if I know I'm going to use this query a lot, I can save it as a view. I can simply do this. I can say CREATE VIEW. And name it trackView.
And then here's just an overloading of the as keyword. As. And this is the view itself. I like to put the semicolon on a separate line when I indent like that. It's just me, it's not necessary. And now I can say select asterisk FROM TrackView, as if it were a table, or a sub select. I press Go. I've got exactly the same result, but now I have got it as a trackView. I don't need to have all of this anymore, I can just say select asterisk from trackView and I get that result.
Now because this select is effectively a table, this trackView used as a SELECT. And it can be used exactly as you would use a table. I can use this as I would use a subselect. Anywhere otherwise, I would use a table. For example, I can do this. So A would be the album table. And so we got a.artist and t.track_number. And T will be our trackView. And title from the trackView.
And minutes and seconds from the trackView from album is A. And join. And we'll grab our track view here, as t on t.album ID equals a dot ID. And we could ORDER BY. And I'll put my semicolon down on another line like that. And when I press Go now I have this lovely joined table again.
I've got my album and my sequence in the album. I've got the minutes and seconds because instead of the track table I'm using this trackView that has that minutes and seconds split out like that. Very useful. When you're done with the View, you can drop it just like you would a table. You can say DROP VIEW trackView. This does not affect the underlying tables. It only deletes the View. So a View is really just a saved query. Because the result of a select is effectively a table, you can use a view anywhere you would use a table or a sub select.
I'll show you more complex example in the next lesson.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98243 Viewers
61 Video lessons · 85549 Viewers
71 Video lessons · 69504 Viewers
56 Video lessons · 101842 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.