UNION

To form a union between two tables, use the following syntax: 

SELECT column(s)
FROM table1
WHERE conditions(s)
UNION
SELECT column(s)
FROM table2
WHERE condition(s)
ORDER BY column(s);

The previous syntax shows you how to UNION two queries together. The WHERE clause in each query is optional. The ORDER BY clause is also optional, and can only appear after the last query in the UNION join. 

If you want to get all the awards for both managers and players in 1994, execute this query: 

SELECT am.playerid, namegiven, awardid, yearid FROM lahmansbaseballdb.awardsmanagers am
INNER JOIN lahmansbaseballdb.people p
ON p.playerid = am.playerid
WHERE yearid = 1994
UNION
SELECT ap.playerid, namegiven, awardid, yearid FROM lahmansbaseballdb.awardsplayers ap
INNER JOIN lahmansbaseballdb.people p
ON p.playerid = ap.playerid
WHERE yearid = 1994
ORDER BY awardid;

The previous query will give you the results shown in the following screenshot:

The previous results are the UNION of the manager awards and the player awards for 1994

Note that you need to use the WHERE clause on each query, but the ORDER BY clause can only be used on the final query. If you didn't have the same number, order, and type on your columns, it would give you an error. Run the following code on the first SELECT statement: 

SELECT am.playerid, awardid, yearid FROM lahmansbaseballdb.awardsmanagers am

This results in the following error:

Error Code: 1222. The used SELECT statements have a different number of columns

This is because you don't have the same number of columns. 

Instead, run the following code on the first SELECT statement: 

SELECT am.playerid, awardid, yearid, namegiven FROM lahmansbaseballdb.awardsmanagers am

You wouldn't get an error in this case, but instead, MySQL would implicitly convert yearid to the same column type as the namegiven column. There are two types of conversion in MySQL: explicit and implicit. Explicit conversion refers to when you explicitly change a data type. Explicit conversion is covered more in Chapter 9, Working with Expressions. Implicit conversion happens when MySQL needs to match data types. In the case of the UNION join, if you use a column with a data type of VARCHAR in the first SELECT statement, and then select a column with a data type of SMALLINT in the second statement, MySQL will implicitly convert the SMALLINT data type to VARCHAR. This conversion happens because the data type of the first SELECT statement is used throughout.

You will get some strange results with an implicit conversion, as shown in the following screenshot:

 

You can see in the previous results that now, you have yearid and namegiven mixed in each of the yearid and namegiven columns. This example impresses on you the importance of the order of the columns specified in the SELECT statements. If MySQL couldn't convert the columns to the same type, then it would give you an error. This brings up an important point in general with query writing, which is that just because you don't get an error, this doesn't mean you got what you wanted. 

You may want to know whether the playerid is associated with a manager or an actual player. To do this, you can add a static column value to your query to note whether the row is a manager or player. You can execute the following query to see how this works: 

SELECT am.playerid, namegiven, awardid, yearid, "Manager" as playeridType 
FROM lahmansbaseballdb.awardsmanagers am
INNER JOIN lahmansbaseballdb.people p
ON p.playerid = am.playerid
WHERE yearid = 1994
UNION
SELECT ap.playerid, namegiven, awardid, yearid, "Player"
FROM lahmansbaseballdb.awardsplayers ap
INNER JOIN lahmansbaseballdb.people p
ON p.playerid = ap.playerid
WHERE yearid = 1994
ORDER BY awardid;

The previous query will give the results shown in the following screenshot:

You can see that by adding a static column in each SELECT statement, you wind up with a column that has that static value for each row. For example, on the awardsmanagers table, you added Manager as a playeridType value, and on the awardsplayer table, you added Player. These values show up as a column in the results. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.42.163