Download presentation
Presentation is loading. Please wait.
1
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
2
© 2010, Mike Murach & Associates, Inc.
Graeme Birchall DB2 SQL Cookbook Reminder: Great Reference Guide The guide is DB2 oriented, but has many good query examples that are applicable for most RDB products. Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
3
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
4
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
5
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
6
© 2010, Mike Murach & Associates, Inc.
ajkNote: Use SQL's power to reduce your PHP programming! Examples: You only need certain values (columns) from a table Let SQL select only those rows Data needs sorting SQL can sort the data for you! On the column(s) of your choice Data from two sources needs organization SQL can join the data from multiple tables And select only the fields of interest! Look at the big picture of what the project needs to do Formulate your queries to your advantage Less PHP processing required! Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
7
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
8
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
9
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
10
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
11
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
12
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
13
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
14
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
15
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
16
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
17
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
18
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
19
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
20
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
21
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
22
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
23
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
24
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
25
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
26
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
27
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
28
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
29
© 2010, Mike Murach & Associates, Inc.
Resume 11/04 Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
30
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
31
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
32
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
33
© 2010, Mike Murach & Associates, Inc.
ajkNote: implicit syntax SELECT select_list FROM table1, table2, … WHERE table1.id = table2.id, tablex.id=tabley.id, … … Note: there is an outer join also: Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
34
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
35
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
36
© 2010, Mike Murach & Associates, Inc.
Alternate form: SELECT firstName, lastName, o.orderID, productName, itemPrice, quantity FROM customers c, orders o, orderItems oi, products p WHERE c.customerID = o.customerID, o.orderID = oi.orderID, oi.productID = p.productID ORDER BY o.orderID Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
37
© 2010, Mike Murach & Associates, Inc.
Note: there are others, e.g. FIRST(), LAST()and some special use functions like VAR() and CORRELATION() Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
38
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
39
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
40
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
41
© 2010, Mike Murach & Associates, Inc.
Example: book_apps\ch18_db2_scripts\fig18-12.sql run without the group by and having first Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
42
© 2010, Mike Murach & Associates, Inc.
w3schools example: Notes: WHERE: Can refer to any column in any of the tables Cannot access aggregate terms HAVING: Can only refer to columns listed in the SELECT condition Can access the aggregate data Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
43
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
44
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
45
© 2010, Mike Murach & Associates, Inc.
book_apps\ch18_db2_scripts\fig18-13.sql run subquery first, then both Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
46
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
47
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
48
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
49
© 2010, Mike Murach & Associates, Inc.
Key elements: The number of items between the ( ) must match the number of columns in the table. There may be as many ( ) as rows you want to insert. If there is no column list then it is assumed the values will be for all the columns in the table. Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
50
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
51
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
52
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
53
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
54
© 2010, Mike Murach & Associates, Inc.
Murach's PHP and MySQL, C18 © 2010, Mike Murach & Associates, Inc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.