Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced select statement Join Other DML commands

Similar presentations


Presentation on theme: "Advanced select statement Join Other DML commands"— Presentation transcript:

1 Advanced select statement Join Other DML commands
LECTURE SIX Advanced select statement Join Other DML commands

2 SELECTING FROM MULTIPLE TABLES
You are not limited to selecting from only one table. When you select from more than one table in one select statement, you are said to be joining tables together. When you want to select from both tables at once, there are a few differences in the syntax of the select statement. You need to ensure that all the tables you are using appear in the FROM clause of the select statement.

3 SELECTING FROM MULTIPLE TABLES
Suppose you have two tables, fruit and color; you can select all rows from each of the two tables. Fruit Color ID FRUITNAME 1 Apple 2 Orange 3 Grape 4 Banana ID COLORNAME 1 Red 2 Orange 3 Purple 4 Yellow

4 SELECTING FROM MULTIPLE TABLES
Note: When you select from multiple tables, you must build proper WHERE clauses to ensure that you get the result you want. From the fruit and color tables, the query for selecting fruitname and colorname from both tables where the id’s match would be; Mysql>select FRUITNAME, COLORNAME FROM Fruit, Color WHERE Fruit.ID = Color.ID;

5 SELECTING FROM MULTIPLE TABLES
If you meant to select the id from the fruit table, you would use; Mysql>select Fruit.ID, FRUITNAME, COLORNAME FROM Fruit, Color WHERE Fruit.ID = Color.ID;

6 SELECTING FROM MULTIPLE TABLES
Result ID FRUITNAME COLORNAME 1 Apple Red 2 Orange 3 Grape Purple 4 Banana Yellow The above illustration joins two tables using a single SELECT query

7 Using JOIN Several types of joins exist in MySQL; all of which refer to the order in which the tables are put together and results displayed. The type of join used in the previous Example Is called an inner join although it was not written explicitly as such. To re-write the SQL statement using the proper INNER JOIN syntax, you would use: Mysql>select FRUITNAME, COLORNAME FROM Fruit inner join Color ON Fruit.ID = Color.ID;

8 Using JOIN Notice the use of an ON clause instead of the WHERE clause.
Both clauses are synonymous and the ON clause uses any conditions that you would use with the WHERE including the various logical and arithmetic operators FRUITNAME COLORNAME Apple Red Orange Grape Purple Banana Yellow Result table

9 LEFT JOIN Here, all rows from the first table will be returned, no matter if there are matches in the second table or not. Consider the following tables; Table: id 42 45

10 LEFT JOIN Master_name Table id firstname lastname 1 John Smith 2 Jane
3 Jimbo Jones 4 Andy 7 Chris 45 Anna Bell 44 Jimmy Carr 43 Albert 42 Doe

11 LEFT JOIN Using LEFT JOIN, you can see that if a value from the table doesn’t exist, a null will appear in place of the address. Mysql>SELECT firstname, lastname, FROM Master_name left join ON Master_name.id= .id; The result of the query is shown below:

12 LEFT JOIN firstname lastname Email John Smith null Jane Jimbo Jones
Andy Chris Anna Bell Jimmy Carr Albert Doe

13 RIGHT JOIN Works like a LEFT JOIN, but with the table order reversed. When using RIGHT JOIN, all rows from the second table will be returned no matter whether there are matches in the first table or not. Example Mysql>SELECT firstname, lastname, FROM Master_name RIGHT JOIN ON Master_name.id= .id;

14 RIGHT JOIN Result Table
firstname lastname John Doe Anna Bell Result Table Several different types of joins are available in MySQL such as Equi-Join, Cross Join, Straight Join and Natural Join. To find out more about joins, visit the mysql manual at:

15 Other DML Commands UPDATE command is used to modify contents of one or more columns in an existing record. The most basic update syntax is as follows: UPDATE table_name SET column1 = “new value”,Column2 = “new value” [Where some_condition_is_true]; The rules for updating a record are similar to those used when inserting a record:

16 UPDATE Command The data you are entering must be appropriate to the data type of the field and You must enclose your strings in single or double quotes. Example Consider the fruit table below: Id Fruit_name status 1 Apple Ripe 2 Pear Rotten 3 Banana 4 Grape

17 UPDATE Command To update the status of the fruit to “ripe”, use
Mysql>UPDATE fruit SET status = ‘Ripe’; The above query will update all the data in the column in question NOTE: It is important to incorporate the WHERE condition to specify a particular condition. Conditional updates refer to the use of WHERE clauses to match specific records.

18 Conditional Updates When using conditional updates, the same comparison and logical operators can be used such as, equal to, less than, e.t.c. Consider the fruit Example; you could have Mysql>UPDATE fruit SET Fruit_name= ‘carrot’ WHERE Fruit_name = ‘Pear’;

19 The REPLACE Command Another method for modifying records is to use the REPLACE command which is remarkably similar to the INSERT. Example REPLACE INTO table_name (column list) VALUES (Column Values); NOTE: The REPLACE command mimics the action of DELETE and re-insert.

20 The DELETE Command The basic DELETE syntax is: DELETE FROM table_name
[Where Some Condition is true]; An example of a conditional delete using the fruit table is as follows; DELETE from fruit WHERE status = ‘Rotten’;


Download ppt "Advanced select statement Join Other DML commands"

Similar presentations


Ads by Google