For example, two teachers called Bill are returned here whereas only one was returned in the earlier example. That’s because we’re selecting all columns in the table, which results in non-duplicates where there was previously a duplicate. You’ll notice that these statements return more rows than in our first example earlier. That’s the equivalent of the following query: SELECT * FROM Teachers TABLE Statementsįrom MySQL 8.0.19 we can use the UNION clause with the TABLE statement. We can see that both Cathys were returned and all three Bills were returned. This time we got twelve rows instead of the eight that we got in our first example. We can use the ALL keyword to include duplicate values in the results: SELECT TeacherName FROM Teachers So we get the same result that we got without the DISTINCT clause. Here’s an example that explicitly uses the DISTINCT clause: SELECT TeacherName FROM Teachers This is despite the fact that the combined tables actually contain two Warrens, two Cathys, and three Bills (there are two teachers called Cathy, a teacher and a customer called Warren, and two called Bill, as well as one student called Bill). So the above results contain just one each of Warren, Cathy and Bill. In other words, it returns only distinct values by default. The column names are taken from the first SELECT statement.īy default, the UNION clause implicitly applies a DISTINCT operation. We can insert the UNION clause between those two SELECT statements to return all teachers and students: SELECT TeacherName FROM Teachers Suppose we have the following tables: SELECT * FROM Teachers In MySQL, the UNION clause combines the results from multiple queries into a single result set.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |