1 MySQL prepared
2 Retrieving Data
- To use SELECT to retrieve table data, you must give at least two pieces of information — what do you want to SELECT, and from where
2.1 Retrieve a single column
- SELECT prod_name FROM products
2.2 Retrieve multiple columns
- SELECT prod_id, prod_name, prod_price FROM products
2.3 Retrieve all columns
- SELECT * FROM products
2.4 Retrieve different rows
- SELECT vent_id FROM products
- SELECT DISTINCT vent_id FROM products
- DISTINCT applies to all columns, not just the column that precedes it
2.5 Restricted Results
- SELECT prod_name FROM products LIMIT 5
- SELECT prod_name FROM products LIMIT 5, 5
- The first argument is the starting position and the second argument is the number of rows to retrieve
2.6 Use fully qualified table names
3 Sort and retrieve data
3.1 Sorting Data
- Relational database design theory states that if the sorting order is not clearly specified, the order of retrieved data should not be assumed to be meaningful
- SELECT prod_name FROM products
- SELECT prod_name FROM products ORDER BY prod_name
3.2 Sort by multiple columns
- SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price, prod_name
3.3 Specifying the sorting direction
- SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC
- SELECT prod_id, prod_price, prod_name FROM products ORDER BY product_price DESC, prod_name
- The DESC keyword applies only to column names directly preceding it
- SELECT prod_price FROM products ORDER BY prod_price DESC LIMIT 1
4 Filtering Data
4.1 Use the WHERE clause
- SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50
4.2 WHERE clause operator
4.2.1 Checking a Single value
- SELECT prod_name, prod_price FROM products WHERE prod_name = ‘fuses’
- SELECT prod_name, prod_price FROM products WHERE prod_price < 10
- SELECT prod_name, prod_price FROM products WHERE prod_price <= 10