Presentation on theme: "MULTIPLE-TABLE QUERIES Chapter 5 in MySQL. Retrieving data from more than one table (joining tables) SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM,"— Presentation transcript:
MULTIPLE-TABLE QUERIES Chapter 5 in MySQL
Retrieving data from more than one table (joining tables) SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM;
Setting restrictions on queries SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM AND CREDIT_LIMIT = 7500;
There is more than one way to write a query and get the same result SELECT DESCRIPTION FROM ORDER_LINE, PART WHERE ORDER_LINE.PART_NUM = PART.PART_NUM AND ORDER_NUM = 21610; Or if you prefer you may use a different format:
Another example of the same query result SELECT DESCRIPTION FROM PART WHERE PART_NUM IN (SELECT PART_NUM FROM ORDER_LINE WHERE ORDER_NUM = 21610); The subquery is considered first.
A Shortcut to writing out long table names in queries SELECT R.REP_NUM, LAST_NAME, FIRST_NAME, C.CUSTOMER_NUM, CUSTOMER_NAME FROM REP R, CUSTOMER C WHERE R.REP_NUM = C.REP_NUM;
Steps to writing a complex query List in the SELECT clause all of the columns/attributes you want to display. If the name of a column is in more than one table, qualify the name by stating which table it is from List in the FROM class all the tables involved in the query. These are usually the tables that contain the data in the SELECT clause, but be aware if you need to restrict the data by using data in ANOTHER table in a WHERE clause, you will need to include it in the FROM clause.
Steps to writing a complex query Take ONE pair of related tables at a time and indicate in the WHERE clause the condition that relates the tables. Join these conditions with the AND operator. If there are any conditions, include them in the WHERE clause and connect them to the other conditions with the AND operator.
Example: For each part on order, list the part number, number ordered, order number, order date, customer number and name, and the last name of the sales rep. SELECT PART_NUM, NUM_ORDERED, ORDER_LINE.ORDER_NUM, ORDER_DATE, CUSTOMER.CUSTOMER_NUM, CUSTOMER, LAST_NAME HERE WE STATE ORDER_LINE BEFORE ORDER_NUM BECAUSE ORDER_NUM IS IN BOTH THE ORDERS AND ORDER_LINE TABLE. WE WANT TO USE THE VALUE IN THE ORDER_LINE TABLE. THE SAME SITUATION APPLIES TO CUSTOMER_NUM. IT EXISTS IN THE CUSTOMER AND ORDERS TABLE.
Example: For each part on order, list the part number, number ordered, order number, order data, customer number and name, and the last name of the sales rep. FROM ORDER_LINE, ORDERS, CUSTOMER, REP HERE ARE ALL THE TABLES NEEDED TO EXTRACT THE DATA I WANT. WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM AND CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM AND REP.REP_NUM = CUSTOMER.REP_NUM HERE YOU ARE CONNECTING THE DATA IN THE TABLES
The finished query SELECT PART_NUM, NUM_ORDERED, ORDER_LINE.ORDER_NUM, ORDER_DATE, CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME, LAST_NAME FROM ORDER_LINE, ORDERS, CUSTOMER, REP WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM AND CUSTOMER.CUSTOMER_NUM = ORDERS. CUSTOMER_NUM AND REP.REP_NUM = CUSTOMER.REP_NUM;
SET OPERATIONS -UNION You can combine two tables if they have the identical structure. This is usually done by creating selected data from a table based on a condition using two queries and then combining the results.
List the number and name of each customer that either is represented by sales rep 65 or that currently has orders on file, or both. First, design the query for the first restriction: SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = 65 Secondly, design the query for the second restriction using the same structure SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER, ORDERS WHERE CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
Now put them together with the UNION operator SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = 65 UNION SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER, ORDERS WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM;
The UNION operator MySQL will delete duplicates when you use the UNION operator. Note: I do not use INTERSECT, MINUS, or ALL and ANY. They tend to be shortcuts that cause more confusion than they are worth! : )
SPECIAL OPERATIONS Inner Join – These are the queries you are already familiar with. They retrieve only the data that you request that satisfies any WHERE clause restrictions.
SPECIAL OPERATIONS Outer Join: These queries list all of the rows from one of the tables in the join regardless of whether they match the restriction criteria in the WHERE clause. A LEFT outer join includes all the rows from the table on the left (always the first table) of the tables listed. In a RIGHT outer join, all rows from the table on the right (the last table listed) are listed. In a FULL outer join, all rows from both tables are included regardless of whether they match rows from another table. I have NEVER used such a query!!!
SPECIAL OPERATIONS Product – A combination of all rows in the first table and second table. You only select TWO tables. List the attributes you wish to retrieve and the tables that contain them. Leave off the WHERE clause. BEWARE: You can mistakenly make a product by accidently omitting the WHERE clause! SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME, ORDER_NUM, ORDER_DATE FROM CUSTOMER, ORDERS; See result on page 155